Jump to content

Recommended Posts

I am using preg_match_all() to get the content between two parameters.

 

Once I have the content, I can search for a phrase and replace it.

All good, but how do I then put those parameters back into the string in order to output the whole thing as a completed document again? is there an easy way of  doing it?

Link to comment
https://forums.phpfreaks.com/topic/181332-solved-preg_match_all/
Share on other sites

Sounds like you're going to want to use preg_replace_callback() instead. That way you can search a string for something, get the result then inside your callback function you can process and do whatever you want with the matched.

 

If you need an example feel free to ask.

yes, I think that's exactly what I need! Maybe it's because it's 3:22 in the morning and I have been up all night looking at this or perhaps I'm just being thick, but that example is looking really good right now...

 

I have

$fullhtml= preg_replace_callback(' '.$r['word'].' ', $replacement_word, $fullhtml); 

I'm not overly familiar with the preg commands yet.

Here's a basic example:

 

<?php
$text = <<<EOT
Some text here
<b>Replace This</b> Some text here
Some text here
EOT;

$text = preg_replace_callback('~<b>(.+?)</b>~', create_function('$matches', 'return "REPLACED!";'), $text);
echo $text;

/* Output: 
Some text here
REPLACED! Some text here
Some text here
*/

 

In this example we're replacing any <b></b> tags and their contents with "REPLACED!" inside of text.

 

Note that parameter 2, as of PHP 5.3, can be replaced with an anonymous function like so:

 

$text = preg_replace_callback('~<b>(.+?)</b>~', function($matches){ return "REPLACED!";}, $text);

 

Edit: I might have misunderstood your original problem. If you don't need to edit the match of the regular expression based on the match (an example would be just strtoupper()'ing the match) then you're best off just using preg_replace().

Yes, thanks, we're almost there but I still need to output the original HTML so the full output would be:

 

Some text here
<b>REPLACED!</b> Some text here
Some text here

 

Also I need the expression to cope with all HTML tags, and not only specific ones as the alt tags that I want to ignore will always be different. I tried hacking your code to:

$text = preg_replace_callback('~<~>(.+?)</~>~', create_function('$matches', 'return "REPLACED!";'), $text);

(which seemed logical at the time) but just gave errors.

 

Thanks so much for your help so-far. just a little further and we'll have it.

I'm trying to match the "real" content of a page (not anything within the HTML code) to a database array of words to replace any matches with the database words and output the amended HTML, including the tags and the replaced content.

Okay... I have:

$html = '<b alt="swear_word">The word "swear_word" needs replacing with something less offensive.</b>';
$pattern = '@\>(.+?)\<@';

$replacement = '/>'.str_replace('swear_word','nice_word',$html).'</';

$page = preg_replace($pattern, $replacement, $html);

echo $page;

 

I then want to replace the "swear_word" with "nice_word", but leave the <b alt="swear_word"> intact.

 

Currently it's just outputting:

<b alt="swear_word"/><b alt="nice_word">The word "nice_word" needs replacing with something less offensive.</b><//b>

 

 

I think you are saying you want to have a word filter but you want the word filter to ignore anything inside an html tag?

 

$string = preg_replace('~((?!<[^>]*))swear_word~','{$1}nice_word',$string);

 

If that is not what you mean, try posting back to back a couple of "before" and "after" examples like

 

BEFORE

AFTER

 

BEFORE

AFTER

 

BEFORE

AFTER

Yes, that's close to what I want but this code:

$html = '<b alt="swear_word">The word "swear_word" needs replacing with something less offensive.</b>';
$page = preg_replace('~((?!<[^>]*))swear_word~','nice_word',$html);
echo $page;

gives the result:

<b alt="nice_word">The word "nice_word" needs replacing with something less offensive.</b><br /> 

 

I want this result instead:

<b alt="swear_word">The word "nice_word" needs replacing with something less offensive.</b><br /> 

This should do it:

 

<?php
$html = '<b alt="swear_word">The word "swear_word" needs replacing with something less offensive.</b>';
$page = preg_replace('~\bswear_word\b(?![^<]*?>)~i', 'nice_word', $html);
echo $page;
//<b alt="swear_word">The word "nice_word" needs replacing with something less offensive.</b>
?>

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.