-1

I need some special filtering to certain text all over my website, like below:

function special_text( $content ) {
    $search_for = 'specialtext';
    $replace_with = '<span class="special-text"><strong>special</strong>text</span>';
    return str_replace( $search_for, $replace_with, $content );
}    
add_filter('the_content', 'special_text', 99);

It's doing thing in an excellent way, BUT...

in content if there's any link like: <a title="specialtext" href="http://specialtext.com">specialtext</a> then the title and href texts also changed and the link becomes broken.

How can I make exception there?

Is there a way I can put some exceptions in an array and str_replace() simply skip 'em?

Mayeenul Islam
  • 4,532
  • 5
  • 49
  • 102

3 Answers3

2

You should use regular expression and use function preg_replace() to replace matched string. Here is the full implementation of your special_text() function.

function special_text( $content ) {
    $search_for = 'specialtext';
    $replace_with = '<span class="special-text"><strong>special</strong>text</span>';
    return preg_replace( '/<a.*?>(*SKIP)(*F)|'.$search_for.'/m', $replace_with, $content );
}  

In the following regular expression first, using <a.*?> - everything between <a...> is matched and using (*SKIP)(*F)| it is skipped and then from anything else $search_for is matched (in your case it's specialtext).

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
  • I might not be testing it correctly, but this doesn't seem to replace the `specialtext` between the `a` tags. OP seemingly only wants the text preserved if it is part of a tag attribute. Tested [here](http://sandbox.onlinephpfunctions.com/code/d765cf463617549493fb2a278f09e197df5a110b). – Patrick Q Jul 24 '14 at 20:05
  • Thanks @PatrickQ I've misread this part. Now it should work fine. – Marcin Nabiałek Jul 24 '14 at 20:11
  • @MarcinNabiałek: Thank you. But I forgot to mention that, my project folder is also with such a name of my text i.e. `specialtext`. So the absolute path contains the string too. And it's not skipping the `` so the images are broken. :( - (But for anchor tags it's working just fine.) – Mayeenul Islam Jul 25 '14 at 17:46
  • @MayeenulIslam is it strictly connected to this issue? If so, please add details, if not, you should simply create one more question with current state and mentioning what should be changed – Marcin Nabiałek Jul 25 '14 at 19:17
1

Jezzabeanz quite got it except you can simplify it still with:

return preg_replace("/^def/", $replace_with, $content);
0

If you just want to change the text between the the a tags then a regular expression works wonders.

Here is something I used when I was pulling data from emails sent to me:

(?<=">)(.*?\w)(?=<\/a)

returns "specialtext"

It also returns "specialtext test" if there is whitespace.

Regular expressions are definitely the way to go.

$subject = "abcdef";
$pattern = '/^def/';
preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE, 3);
print_r($matches);
?>

Source

And then do a replace on the returned matches.

ZeroBased_IX
  • 2,667
  • 2
  • 25
  • 46
  • See some of the answers to [this question](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/) for reference as to why regex is _rarely_ what you want to use for parsing HTML. – Patrick Q Jul 24 '14 at 19:39
  • "it's sometimes appropriate to parse a limited, known set of HTML." If Mayeenul is only replacing the text between hyperlinks then this falls under the band of "limited, known set...". – ZeroBased_IX Jul 24 '14 at 19:43
  • Yes, but without that being explicitly stated as what's wanted, I don't think it is a safe assumption. Especially given "... all over my website ..." – Patrick Q Jul 24 '14 at 19:44
  • Yes very true, I was taking his example literally. The regex example I gave was limited, true, but he can certainly do what he needs to do with a regular expression. If he want's something a little more comprehensive then he's better off using the DOM Parser as you suggested in the comment you gave on the OP. – ZeroBased_IX Jul 24 '14 at 19:52
  • @Jezzabeanz it won't work if `$search_for = 'specialtext';` There is no info in OP question that only text inside a has to be changed. – Marcin Nabiałek Jul 24 '14 at 20:14