Jump to content

[SOLVED] Another (simple) preg_replace problem


immanuelx2

Recommended Posts

You mean something along these lines?

 

$str = 'Some text... $var Some more text... [$var] Even more text.. finally, $var and [$var]!';

$pattern = '/(^|\])[^[]+/';
$variable = '$var';
function replace($a){
global $variable;
return str_replace($variable, '*whatever*', $a[0]);
}
$str = preg_replace_callback($pattern, 'replace', $str);
echo $str;

 

Output:

Some text... *whatever* Some more text... [$var] Even more text.. finally, *whatever* and [$var]!

Well, those 'extra steps' takes what preg finds (outside the [] characters), then to the replace function, uses str_replace to replace any $variable found. A regular preg_replace alone would not do all that.

 

All you need to do is change a) the $variable that you want to see replaced, and b) $str to the string you want to check.

Well, those 'extra steps' takes what preg finds (outside the [] characters), then to the replace function, uses str_replace to replace any $variable found. A regular preg_replace alone would not do all that.

 

All you need to do is change a) the $variable that you want to see replaced, and b) $str to the string you want to check.

 

Hmm. Thanks for the reply... Here is my delimma though

 

I am trying to get a search highlighted fields script going, and right now I have

 

$article_short = preg_replace("/\b(".$_GET['search'].")\b/i", '<span style="background:#ffffab">\1</span>', $article['short']);

 

that goes through and encloses all instances of $_GET['search'] with that span tag. However, the problem is that if there is a with the variable present, it will replace the URL with span tags and mess it up.

 

So how can I do that? Your example used str_replace which I don't think will work for my situation.

Well I suppose we could exchange str_replace for a version of your preg_replace like so:

 

Example:

$article['short'] = 'Some text... Car Some more text... [Car] Even more text.. finally, Car and [Car]!';

$pattern = '#(^|\])[^[]+#';
$_GET['search'] = 'some'; // Obviously, you won't use this line, as you have your $_GET value.. I just use this as a test

function replace($a){
return preg_replace('#\b('.$_GET['search'].')\b#i', '<span style="background:#ffffab">\1</span>', $a[0]);
}

$article_short = preg_replace_callback($pattern, 'replace', $article['short']);
echo $article_short;

Well I suppose we could exchange str_replace for a version of your preg_replace like so:

 

Example:

$article['short'] = 'Some text... Car Some more text... [Car] Even more text.. finally, Car and [Car]!';

$pattern = '#(^|\])[^[]+#';
$_GET['search'] = 'some'; // Obviously, you won't use this line, as you have your $_GET value.. I just use this as a test

function replace($a){
return preg_replace('#\b('.$_GET['search'].')\b#i', '<span style="background:#ffffab">\1</span>', $a[0]);
}

$article_short = preg_replace_callback($pattern, 'replace', $article['short']);
echo $article_short;

 

This worked :)

 

Thanks a lot for helping with this complex problem. However, now I realize that it still return articles if a match is found in the URLS... so I have to figure out a way for MySQL to recognize where the URLS are and not include them in the LIKE '%search%' statement

 

:(

Archived

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

×
×
  • 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.