Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace all quotes that are not in html-tags

currently i am replacing all my quotes inside a text with special quotes. But how can i change my regex that only quotes inside the text will be replaced and not the ones who are used in html tags.

$text = preg_replace('/"(?=\w)/', "»", $text);
$text = preg_replace('/(?<=\w)"/', "&laquo;", $text);

I am not that fit in regular expressions. The problem is that i need to replace the starting quotes with another symbol than ending quotes.

If you do need more information, say so.

Any help is appreciated!

EDIT

Test Case

<p>This is a "wonderful long text". At least it should be. Here we have a <a href="http://wwww.site-to-nowhere.com" target="_blank">link</a>.</p>

The expected output should be:

<p>This is a &raquo;wonderful long text&laquo;. At least it should be. Here we have a <a href="http://wwww.site-to-nowhere.com" target="_blank">link</a>.</p>

Right now it is like this:

<p>This is a &raquo;wonderful long text&laquo;. At least it should be. Here we have a <a href=&raquo;http://wwww.site-to-nowhere.com&laquo; target=&raquo;_blank&laquo;>link</a>.</p>

EDIT 2

Thx for the answer of Kamehameha i've added the following code to my script:

$text = preg_replace("/\"([^<>]*?)\"(?=[^>]+?<)/", "&raquo;\1&laquo;", $text);

What worked great in the regex tester does not replace anything. Did i do anything wrong?

like image 681
Tobias Golbs Avatar asked Mar 23 '14 10:03

Tobias Golbs


People also ask

How do you replace double quotes in HTML?

$cHTML = str_replace( '"','&quote;', preg_replace_callback('/[^\s][=].

How do you remove quotes from a string?

To remove double quotes from a string:Call the replace() method on the string. The replace method will replace each occurrence of a double quote with an empty string. The replace method will return a new string with all double quotes removed.


2 Answers

This regex works for the given strings.

Search for   - "([^<>]*?)"(?=[^>]*?<)
Replace with - &raquo;\1&laquo;

Demo here
Testing it -

INPUT - 
<p>This is a "wonderful long text". "Another wonderful ong text" At least it should be. Here we have a <a href="http://wwww.site-to-nowhere.com" target="_blank">link</a>.</p>

OUTPUT - 
<p>This is a &raquo;wonderful long text&laquo;. &raquo;Another wonderful ong text&laquo; At least it should be. Here we have a <a href="http://wwww.site-to-nowhere.com" target="_blank">link</a>.</p>

EDIT 1-
Executing this in PHP -

$str = '<p>This is a "wonderful long text". "Another wonderful ong text" At least it should be. Here we have a <a href="http://wwww.site-to-nowhere.com" target="_blank">link</a>.</p>';
var_dump(preg_replace('/"([^<>]*?)"(?=[^>]*?<)/', '&raquo;\1&laquo', $str));

It's output -

/** OUTPUT **/
string '<p>This is a &raquo;wonderful long text&laquo. &raquo;Another wonderful ong text&laquo At least it should be. Here we have a <a href="http://wwww.site-to-nowhere.com" target="_blank">link</a>.</p>' (length=196)

EDIT 2-
You have executed the preg_replace function properly, but in the replacement string, you have used \1 inside the Double quotes(""). Doing so, you are escaping the 1 itself and that won't be replaced.
To make it more clear, try this and see what happens -

echo '&raquo;\1&laquo;';
echo "&raquo;\1&laquo;";

The second \1 should not be visible.
So the solution would be one of these -

preg_replace('/"([^<>]*?)"(?=[^>]*?<)/', '&raquo;\1&laquo;', $str)
preg_replace("/\"([^<>]*?)\"(?=[^>]*?<)/", "&raquo;\\1&laquo;", $str)
preg_replace("/\"([^<>]*?)\"(?=[^>]*?<)/", "&raquo;$1&laquo;", $str)

Read the Replacement section in this page for more clarity.

EDIT 3-
A regex that covers text which might not be enclosed within tags-

\"([^<>]*?)\"(?=(?:[^>]*?(?:<|$)))

Demo here

like image 142
Kamehameha Avatar answered Sep 19 '22 14:09

Kamehameha


Could also use a negative lookahead:

(?![^<]*>)"([^"]+)"

Replace with: &raquo;\1&laquo;

like image 26
Jonny 5 Avatar answered Sep 21 '22 14:09

Jonny 5