Shiplu wrote:
The string is "<td>charge</td><td>100</td>".
I want and array( "charge"=>100).
I am using this regular expression,
'/<td>([^<]+)<\/td><td>(?P<\1>\d+)<\/td>/'.


But its not working..

I get this error.,
PHP Warning:  preg_match(): Compilation failed: syntax error after (?P
at offset 25 in E:\src\php\WebEngine\- on line 4

any idea?


it seem's everybody is giving you the same answers; here's a definitive look at the problem (as far as I'm aware)

all preg_* functions can only return back string's, or array's of strings; there is no method of returning back an associative array as you would like; the closest you can get is by using preg_replace or preg_replace_callback as follows:

print_r( preg_replace_callback('|<td>(.*)</td><td>(\d+)</td>|', create_function(
            '$matches',
            'return array($matches[0],$matches[1]);'
        ), $string) );

this will fall as the internals of preg* casts the array to a string

alternative:

print_r( preg_replace('|<td>(.*)</td><td>(\d+)</td>|e', 'array("$1","$2")', $string) );

this will also fail as the internals of preg* casts the array to a string.

similarly all other options you could go down such as simple explodes, strip_tags or even more complex stream filters will only allow you to return strings, or numerical array's of strings.

This leaves you high and dry I'm afraid - the only thing for it is to create a simple function to handle this for you; something like

function td_thing( $string ) {
$a = preg_replace('|<td>(.*)</td><td>(\d+)</td>|e', '$1 $2', $string);
$b = explode(' ', $a);
return array("$b[0]" => "$b[1]);
}

maybe just maybe you or I or somebody else can find a way to do this easily in one line; but for now a function similar to above is the best you can do..

Regards

Nathan

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to