Replace all quotes that are not specified in html tags

Currently, I am replacing all my quotes inside the text with special quotes. But how can I change my regular expression, which will be replaced only with quotation marks inside the text, and not those used in html tags.

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

I'm not used to regular expressions. The problem is that I need to replace the start quotes with a different character than end the quotes.

If you 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 result 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>

Now it looks 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

thanks for Kamehameha's answer, I added the following code to my script:

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

What worked great in the regex test doesn't replace anything. Did I do something wrong?

+4
4

.

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


-

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>

1 -
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));

-

/** 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)

2 -
preg_replace, \1 ( "). , 1, .
, , -

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

\1 .
, -

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

"" .

3 -
, , -

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

+6

:

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

: &raquo;\1&laquo;

+1

PHP-, <a...</a>.

: <a.*?<\/a>(*SKIP)(*F)|"([^"]*)"

: &raquo;\1&laquo;

Demo .

( ) , s1, s2, s3...

+1

:

(?<=^|>)[^><]+?(?=<|$)

html.

0

Source: https://habr.com/ru/post/1533044/


All Articles