bilis_money Posted August 11, 2006 Share Posted August 11, 2006 hii'm trying to match a word inside a string.and here are the codes,[code]<?php $title_old = "The payment system is currently unavailable while we perform a system upgrade. Bringing these features back is our top priority, and we expect to have them restored shortly."; $title_new = "online"; //compare and search new_topic with old_topic using REGEX if(preg_match("/\b$topic_new\b/i", $title_old)) { echo "Yeah, i saw it!<br>"; } else { echo "I can't see it!"; }?>[/code]what i'm trying to do here is to just match the word 'online' witht the stringcontent of $title_old.now i'm not sure of inserting those variables inside preg_match, can youguys check if if i'm inserting $title_new and $title_old inside preg_match() correctly? Thank you very much in advance. Link to comment https://forums.phpfreaks.com/topic/17266-matching-word-into-string-is-this-correct-variable-insertion/ Share on other sites More sharing options...
corbin Posted August 11, 2006 Share Posted August 11, 2006 $title_old = "The payment system is currently unavailable while we perform a system upgrade. Bringing these features back is our top priority, and we expect to have them restored shortly.";I dont see online in there.... Link to comment https://forums.phpfreaks.com/topic/17266-matching-word-into-string-is-this-correct-variable-insertion/#findComment-73240 Share on other sites More sharing options...
corbin Posted August 11, 2006 Share Posted August 11, 2006 http://us2.php.net/preg_match Link to comment https://forums.phpfreaks.com/topic/17266-matching-word-into-string-is-this-correct-variable-insertion/#findComment-73243 Share on other sites More sharing options...
zq29 Posted August 11, 2006 Share Posted August 11, 2006 If you're only doing simple searches like that, it would be more efficient to use strpos().[code]<?php$title_old = "The payment system is currently unavailable while we perform a system upgrade. Bringing these features back is our top priority, and we expect to have them restored shortly."; $title_new = "online"; if(strpos($title_old,$title_new) !== false) { echo "Yeah, i saw it!";} else { echo "I can't see it!";}?>[/code] Link to comment https://forums.phpfreaks.com/topic/17266-matching-word-into-string-is-this-correct-variable-insertion/#findComment-73247 Share on other sites More sharing options...
bilis_money Posted August 11, 2006 Author Share Posted August 11, 2006 thanks SA but i need it in REGEX,I think REGEX is the best when it comes to this issue.I'm trying to learn and absorb REGEX, because i believe it is complete and robust when it comes to this matter.Hey corbin i'm studying REGEX for months now i don't think you understood my question.If you tried to execute that codes you'll see what i mean.i suggest you read it carefully and understand it first before you make your comments, but anyway thank you also.ok, what i mean with that code is it is not suppose to display the 'I saw it!' echo because 'online' word is not inside the string.So i guess there is wrong with my preg_match() Link to comment https://forums.phpfreaks.com/topic/17266-matching-word-into-string-is-this-correct-variable-insertion/#findComment-73265 Share on other sites More sharing options...
effigy Posted August 11, 2006 Share Posted August 11, 2006 Use [tt]$title_new[/tt] instead of [tt]$topic_new[/tt]. Learning regular expressions is important, but knowing when to use them carries just as much weight. Link to comment https://forums.phpfreaks.com/topic/17266-matching-word-into-string-is-this-correct-variable-insertion/#findComment-73274 Share on other sites More sharing options...
bilis_money Posted August 11, 2006 Author Share Posted August 11, 2006 Ha!,ha!,ha.. :D Lol! my mistake....! very funny.....Thanks effigy... i didn't notice that.I inserted the wrong variable name... THanks! Link to comment https://forums.phpfreaks.com/topic/17266-matching-word-into-string-is-this-correct-variable-insertion/#findComment-73278 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.