mysterbx Posted March 25, 2009 Share Posted March 25, 2009 Hello, How can i find "..." (3 dots) in a text? I tried using preg_match("#\.\.\.#ise",$x); it didnt work, tried preg_match("/\.\.\./i",$x); - also nothing. What is wrong with that preg match? Quote Link to comment https://forums.phpfreaks.com/topic/151149-find-in-the-text/ Share on other sites More sharing options...
lonewolf217 Posted March 25, 2009 Share Posted March 25, 2009 do you just want to know the existence of the 3 dots, or find the position, or what else ? Quote Link to comment https://forums.phpfreaks.com/topic/151149-find-in-the-text/#findComment-794022 Share on other sites More sharing options...
unska Posted March 25, 2009 Share Posted March 25, 2009 Regexp is better and faster way for doing it but if you want an easier way you can use the following code. <?php $string = "Hello my name is ... and i like potatoes."; $toFind = "..."; $position = strpos($string, $toFind); if ($position !== false) { echo "The three dots start at this position: ".$position; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/151149-find-in-the-text/#findComment-794024 Share on other sites More sharing options...
Maq Posted March 25, 2009 Share Posted March 25, 2009 $s = "timsgsdaa...asdfasdf"; if(preg_match("/\.{3}/", $s)) { echo "YAY"; } else { echo "NOO"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/151149-find-in-the-text/#findComment-794049 Share on other sites More sharing options...
br0ken Posted March 25, 2009 Share Posted March 25, 2009 Unska's code works the best. Regex isn't faster in working with strings. It is more powerful than string functions but slower. Quote Link to comment https://forums.phpfreaks.com/topic/151149-find-in-the-text/#findComment-794054 Share on other sites More sharing options...
unska Posted March 25, 2009 Share Posted March 25, 2009 I've always thought regexp is faster.. Well, you learn something new everyday. Quote Link to comment https://forums.phpfreaks.com/topic/151149-find-in-the-text/#findComment-794056 Share on other sites More sharing options...
bluejay002 Posted March 26, 2009 Share Posted March 26, 2009 Yep... unska's way is better. If something could be accomplished using simple string function, then use it. It's faster with that and pattern matching are supposed to be used on complex ones to make you code more efficient. Quote Link to comment https://forums.phpfreaks.com/topic/151149-find-in-the-text/#findComment-794083 Share on other sites More sharing options...
mysterbx Posted March 26, 2009 Author Share Posted March 26, 2009 cool, thanks a lot, everything runs much smoother now Quote Link to comment https://forums.phpfreaks.com/topic/151149-find-in-the-text/#findComment-794471 Share on other sites More sharing options...
redarrow Posted March 26, 2009 Share Posted March 26, 2009 The best code is always preg_match() that what it for . Quote Link to comment https://forums.phpfreaks.com/topic/151149-find-in-the-text/#findComment-794476 Share on other sites More sharing options...
Maq Posted March 26, 2009 Share Posted March 26, 2009 The best code is always preg_match() that what it for . Not true, check out some benchmark tests, it will show, for this specific example, that strpo() is faster then the preg_match() function. But preg_match is much much more powerful, like br0ken already mentioned. Quote Link to comment https://forums.phpfreaks.com/topic/151149-find-in-the-text/#findComment-794481 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.