Jump to content

matching word into string? is this correct variable insertion?


bilis_money

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.

Link to comment
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]
Link to comment
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()

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.