Jump to content


Photo

matching word into string? is this correct variable insertion?


  • Please log in to reply
6 replies to this topic

#1 bilis_money

bilis_money
  • Members
  • PipPipPip
  • Advanced Member
  • 621 posts

Posted 11 August 2006 - 05:52 PM

hi

i'm trying to match a word inside a string.
and here are the codes,
<?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!";
	}
?>

what i'm trying to do here is to just match the word 'online' witht the string
content of $title_old.
now i'm not sure of inserting those variables inside preg_match, can you
guys check if if i'm inserting $title_new and $title_old inside preg_match() correctly?

Thank you very much in advance.



#2 corbin

corbin
  • Staff Alumni
  • Advanced Member
  • 8,129 posts

Posted 11 August 2006 - 05:54 PM

$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....
Why doesn't anyone ever say hi, hey, or whad up world?

#3 corbin

corbin
  • Staff Alumni
  • Advanced Member
  • 8,129 posts

Posted 11 August 2006 - 05:55 PM

http://us2.php.net/preg_match
Why doesn't anyone ever say hi, hey, or whad up world?

#4 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 11 August 2006 - 05:59 PM

If you're only doing simple searches like that, it would be more efficient to use strpos().

<?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!";
}
?>


#5 bilis_money

bilis_money
  • Members
  • PipPipPip
  • Advanced Member
  • 621 posts

Posted 11 August 2006 - 06:25 PM

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()



#6 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 11 August 2006 - 06:40 PM

Use $title_new instead of $topic_new. Learning regular expressions is important, but knowing when to use them carries just as much weight.
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#7 bilis_money

bilis_money
  • Members
  • PipPipPip
  • Advanced Member
  • 621 posts

Posted 11 August 2006 - 06:45 PM

Ha!,ha!,ha..  :D Lol! my mistake....! very funny.....

Thanks effigy... i didn't notice that.

I inserted the wrong variable name...

THanks!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users