简体   繁体   中英

PHP regular expressions and XML

I have an XML database of 17000 records that are not well formed. The problem with the XML is that the attributes are not in quotes ("").

My script collects only one record.

The problem is to put the attribute values within quotes.

So if I have a string like:

$str = "This is a test id=abc> string";
$str1 = '="';
$str2 = str_replace("=", $str1, $str);

My problem is to to get the attribute value within the quotes ( id="abc"> ). I tried using regular expressions, but that didn't work.

<?php

$string = "This is a test id=abc> string";
$replace = preg_replace('/=([\w]+)/i', '="$1"', $string );

echo $replace ;
//This is a test id="abc"> string
?>

http://ideone.com/OurhHV

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM