Jump to content

Archived

This topic is now archived and is closed to further replies.

bilis_money

matching word into string? is this correct variable insertion?

Recommended Posts

hi

i'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 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.

Share this post


Link to post
Share on other sites
$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....

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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()

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Ha!,ha!,ha..  :D Lol! my mistake....! very funny.....

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

I inserted the wrong variable name...

THanks!

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.