+ 1

Breaking down strings in php

A string contains (n) amount of words letters or symbols.The string contains data inserted by the user. Can I break down the string and search for a specific word, phrase or symbol in the string and echo something if the the user has typed what I was searching for. If there is a way please explain. Thanks 😀

27th Apr 2017, 4:37 PM
Mashan Shaluka1
Mashan Shaluka1 - avatar
4 Respostas
+ 3
try this: $string = "this is a sample string"; $arr = explode(' ' , $string); foreach ( $arr as $elem ){ if ( $elem == "sample" ) { echo "ok, is just a sample"; } }
27th Apr 2017, 4:55 PM
⏩▶Clau◀⏪
⏩▶Clau◀⏪ - avatar
+ 3
Make sure that your delimiter is a space and not an empty string.
27th Apr 2017, 5:17 PM
CHMD
CHMD - avatar
0
@Claudio Vidal I copied the exact code above but it does not seem to work. errors: explode() : empty delimiter invalid argument supplied for forearch()
27th Apr 2017, 5:05 PM
Mashan Shaluka1
Mashan Shaluka1 - avatar
0
@kingdx works now 😃 does explode break down the to words?
27th Apr 2017, 5:28 PM
Mashan Shaluka1
Mashan Shaluka1 - avatar