Hi i want to replace all " citations for citation simple ' but not when is a anchor
for example this
Lorem "ipsum" dolor sit amet, <a href="#">sit amet</a>
to this
Lorem 'ipsum' dolor sit amet, <a href="#">sit amet</a>
im doing this $valFieldBody = str_replace('"', "''", $valFieldBody);
but that replace all " i dont know if i can use str_replace
or preg_replace
funcion with negative condition like !=
or <>
could you please help me
thanks
The preg_replace function allows you to make multiple combinations to replace, in this case I look for the string that replaces only accepts spaces and alphanumeric characters that are inside ( " " ) in your case the string "ipsum", can be extended to other characters if desired
Example with your string
Update code:
$text='Lorem "ipsum" dolor sit amet, <a href="#">sit amet</a>';
$text = preg_replace('/\"([A-Za-z0-9? ,_-]+)\"(?=[^<>]*(?:<|$))/', "'$1'", $text);
echo $text;
//out--> Lorem 'ipsum' dolor sit amet, <a href="#">sit amet</a>
You may use a SKIP-FAIL regex:
preg_replace('~<[^>]+>(*SKIP)(*F)|"~', "'", $valFieldBody);
See the regex demo.
Pattern details
<[^>]+>
- matches <
, 1+ chars other than >
, and then >
(*SKIP)(*F)
- 2 PCRE verbs omitting the matched text and making the regex engine search for the next match after the end of the current match|
- or"
- a double quote in other contexts.