开发者

String parsing help

开发者 https://www.devze.com 2022-12-18 16:33 出处:网络
I have a paragraph of text in the following format: text text text <age>23</age>. text text <hobbies>...</hobbies>

I have a paragraph of text in the following format:

  text text text <age>23</age>. text text <hobbies>...</hobbies>

I want to be able to

1) Extract the text found between each <age> and <hobbies> tag found in the string. So for example, I would have an array called $ages which will contain all ages found between all the <age></age> tags, and then another array $hobbies which will have the text between the <hobbies></hobbies> tags found throughout the string.

2) Be 开发者_StackOverflowable to replace the tags which are extracted with a marker, such as {age_444}, so e.g the above text would become

  text text text {age_444}. text text {hobbies_555}

How can this be done?


//Extract the age
preg_match_all("#<age>(.*?)</age>#",$string,$match);
$ages=$match[1];
//Extract the hobby
preg_match_all("#<hobbies>(.*?)</hobbies>#",$string,$match);
$hobbies=$match[1];

//Replace the age
$agefn=create_function('$match','$query=mysql_query("select ageid...where age=".$match[1]); return "<age>{age_".mysql_fetch_object($query)->ageid."}</age>"');
$string=preg_replace_callback("#<age>(.*?)</age>#",$agefn,$string);

//Replace the hobby
$hobfn=create_function('$match','$query=mysql_query("select hobid...where hobby=".$match[1]); return "<hobbies>{hobbies_".mysql_fetch_object($query)->hobid."}</hobbies>"');
$string=preg_replace_callback("#<hobbies>(.*?)</hobbies>#",$hobfn,$string);


If your source document is a kind of well-formed XML (or if it can easily be brought into this shape at least), you can use XSLT/XSL-FO to transform your document.

Finding informations enclosed by <> tags and rearranging/extracting them is one of the main features. You can use XSLT/XSL-FO stand-alone or within various languages (Java, C, even Visual Basic)

What you need is your source document and a document describing the transformation rules. The rendering machine or library function will do the rest.

Hope that helps. Good luck


$string = '<age>23</age><hobbies>hobbietext</hobbies>';

$ageTemp = explode('<age>', $string );
foreach($ageTemp as $key=>$value)
{
    $age = explode('</age>', $value);
    if(isset($age[0])) $ages[] = $age[0];
}

$hobbiesTemp = explode('<hobbies>', $string );
foreach($hobbiesTemp as $key=>$value)
{
    $hobbie = explode('</hobbies>', $value);
    if(isset($hobbie[0])) $hobbies[] = $hobbie[0];
}

final arrays are $hobbies and $ages

after that you just replace your sting like this:

foreach($ages as $key=>$value)
{
     $string = str_replace('<age>'.$value.'</age>', '{age_'.$yourId.'}', $string);
}

foreach($hobbies as $key=>$value)
{
     $string = str_replace('<hobbies>'.$value.'</hobbies>', '{hobbie_'.$yourId.'}', $string);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号