if I have input like this <n>336197298</n>
how do I get the number between the tag by using php programming. I try to use regular expression but I can't find the method for this task. Could you please help me .
i think regular expression is best try this method,
function get_content( $tag , $content )
{
preg_match("/<".$tag."[^>]*>(.*?)<\/$tag>/si", $content, $matches);
return $matches[1];
}
Assuming there is no nesting of tags, the regex you need is
n >(.*?)<
This captures exactly what is between n >
and <
, but it makes many assumptions that you weren't clear about. Is it always n
or can it be something else? Is there always a single space between the tag name and >
? Are you worried about matching the tags?
Don't use a regex for this.
<?php
$simple = "<para><note>simple note</note></para>";
$p = xml_parser_create();
xml_parse_into_struct($p, $simple, $vals, $index);
xml_parser_free($p);
echo "Index array
";
print_r($index);
echo "
Vals array
";
print_r($vals);
?>
Output:
Index array
Array
(
[PARA] => Array
(
[0] => 0
[1] => 2
)
[NOTE] => Array
(
[0] => 1
)
)
Vals array
Array
(
[0] => Array
(
[tag] => PARA
[type] => open
[level] => 1
)
[1] => Array
(
[tag] => NOTE
[type] => complete
[level] => 2
[value] => simple note
)
[2] => Array
(
[tag] => PARA
[type] => close
[level] => 1
)
)