DamienRoche Posted October 13, 2008 Share Posted October 13, 2008 All I want to do is find a match in a string - the whole string actually. Here's my code: <?php $str = 'echo"<b>Hello the World!<\b>"'; if(!preg_match("#echo\"<\b>Hello the World!<\/\b>\"#", $str, $match)) { echo "<br><b>Couldn't find:</b>".htmlentities($str)."<br>"; } else { echo "found match:".$match[0];} ?> I have no idea where I am going wrong. I'm tried numerous reg ex combinations with escaping and also with the string - things like htmlentities() and stripslashes...I just don't know why I can't find a match. Any help is greatly appreciated. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/128148-where-have-i-gone-wrong-here/ Share on other sites More sharing options...
Zane Posted October 13, 2008 Share Posted October 13, 2008 Remove the stuff in red "#echo\"Hello the World!\"#" Quote Link to comment https://forums.phpfreaks.com/topic/128148-where-have-i-gone-wrong-here/#findComment-663717 Share on other sites More sharing options...
DamienRoche Posted October 13, 2008 Author Share Posted October 13, 2008 Here's my new code: <?php $str = 'echo"<b>Hello the World!<\b>"'; if(!preg_match("#echo\"<b>Hello the World!</\b>\"#", $str, $match)) { echo "<br><b>Couldn't find:</b>".htmlentities($str)."<br>"; } else { echo "found match:".$match[0];} ?> OUTPUT: Couldn't find:echo"<b>Hello the World!<\b>" I have no idea why it's doing this. I can match just text quite easily but as soon as I introduce the <b> tag, it crumbles. Any other ideas? thanks. Quote Link to comment https://forums.phpfreaks.com/topic/128148-where-have-i-gone-wrong-here/#findComment-663722 Share on other sites More sharing options...
Zane Posted October 13, 2008 Share Posted October 13, 2008 well your closing b tag is backwards Hello the World! ... should be Hello the World! and then maybe you could do #echo\"Hello the World!\"# Quote Link to comment https://forums.phpfreaks.com/topic/128148-where-have-i-gone-wrong-here/#findComment-663727 Share on other sites More sharing options...
DamienRoche Posted October 13, 2008 Author Share Posted October 13, 2008 Got it. I tried without escaping b and just using the forward slash and it worked. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/128148-where-have-i-gone-wrong-here/#findComment-663732 Share on other sites More sharing options...
JasonLewis Posted October 13, 2008 Share Posted October 13, 2008 Note your error, \b means word boundary. You can read more about them here: http://www.regular-expressions.info/wordboundaries.html Quote Link to comment https://forums.phpfreaks.com/topic/128148-where-have-i-gone-wrong-here/#findComment-663734 Share on other sites More sharing options...
ghostdog74 Posted October 14, 2008 Share Posted October 14, 2008 why do you need to use preg_match to match a whole string. Use the equality operator == $str1 == $str2 if your string is part of a bigger string, its also simpler to use strpos(). Quote Link to comment https://forums.phpfreaks.com/topic/128148-where-have-i-gone-wrong-here/#findComment-665670 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.