There might be other, and better, solutions, but this is how I made it works:
- Using
preg_match_all
with certain regex to capture all matches, and store them in an array $matches[0]
.
- The regex:
(?<=data_value=").*(?=" \/>)
will capture everything between data_value="
and " />)
, by making use of positive lookbehind and lookahead, precisely match the values of each of the data_value
attributes.
- Loop through items in
$matches[0]
and we do the following:
- Replace every double qoutes string
"
with %
[could be any other string, even blank, that doesn't cause further problems] in every single match, and store it in a temporary variable $str
.
- Then replace the value of each match in the whole data string with the value of the modified version of the match, the
$str
string.
PHP code:
remember that because the data is xml tags, you need to use "view source" in order to see the output, alternatively, you can use var_dump
instead of echo
<?php
$data = '<record ObTime="2017-05-10T23:30" data_value="Ocean Park "The Sea WX" WA US" />
<record ObTime="2017-11-10T23:30" data_value="Some Other "Demo Text" In Here" />';
$data_valueVal = preg_match_all('#(?<=data_value=").*(?=" \/>)#i', $data, $matches);
foreach($matches[0] as $match) {
$str = str_replace('"', "%", $match);
$data = str_replace($match, $str, $data);
}
echo $data;
?>
Output:
<record ObTime="2017-05-10T23:30" data_value="Ocean Park %The Sea WX% WA US" />
<record ObTime="2017-11-10T23:30" data_value="Some Other %Demo Text% In Here" />