gerkintrigg Posted November 13, 2009 Share Posted November 13, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/181332-solved-preg_match_all/ Share on other sites More sharing options...
Alex Posted November 13, 2009 Share Posted November 13, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/181332-solved-preg_match_all/#findComment-956592 Share on other sites More sharing options...
gerkintrigg Posted November 13, 2009 Author Share Posted November 13, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/181332-solved-preg_match_all/#findComment-956610 Share on other sites More sharing options...
Alex Posted November 13, 2009 Share Posted November 13, 2009 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(). Quote Link to comment https://forums.phpfreaks.com/topic/181332-solved-preg_match_all/#findComment-956612 Share on other sites More sharing options...
gerkintrigg Posted November 13, 2009 Author Share Posted November 13, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/181332-solved-preg_match_all/#findComment-956725 Share on other sites More sharing options...
thebadbad Posted November 13, 2009 Share Posted November 13, 2009 So what are you trying to match, and what should be replaced in the matched content? Quote Link to comment https://forums.phpfreaks.com/topic/181332-solved-preg_match_all/#findComment-956878 Share on other sites More sharing options...
gerkintrigg Posted November 14, 2009 Author Share Posted November 14, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/181332-solved-preg_match_all/#findComment-957286 Share on other sites More sharing options...
thebadbad Posted November 14, 2009 Share Posted November 14, 2009 Can you be more specific? What are you trying to do? I can't make much sense of it. Quote Link to comment https://forums.phpfreaks.com/topic/181332-solved-preg_match_all/#findComment-957365 Share on other sites More sharing options...
gerkintrigg Posted November 14, 2009 Author Share Posted November 14, 2009 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> Quote Link to comment https://forums.phpfreaks.com/topic/181332-solved-preg_match_all/#findComment-957470 Share on other sites More sharing options...
.josh Posted November 15, 2009 Share Posted November 15, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/181332-solved-preg_match_all/#findComment-957811 Share on other sites More sharing options...
gerkintrigg Posted November 15, 2009 Author Share Posted November 15, 2009 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 /> Quote Link to comment https://forums.phpfreaks.com/topic/181332-solved-preg_match_all/#findComment-957821 Share on other sites More sharing options...
thebadbad Posted November 15, 2009 Share Posted November 15, 2009 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> ?> Quote Link to comment https://forums.phpfreaks.com/topic/181332-solved-preg_match_all/#findComment-957882 Share on other sites More sharing options...
gerkintrigg Posted November 15, 2009 Author Share Posted November 15, 2009 Yes, that's perfect! thanks. Quote Link to comment https://forums.phpfreaks.com/topic/181332-solved-preg_match_all/#findComment-958124 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.