tags:

views:

59

answers:

3

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 able 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?

A: 
$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);
}
dfilkovi
+1  A: 
//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);
mck89
Great answer but I can't use the create_function part. Also my code for getting the id of the age etc will be vastly different. Is there any other way to replace the age? I would prefer if the character position of the age/hobbies could be retrieved while looping through $ages/$hobbies, so i can then use substr_replace() to replace the age/hobby
Click Upvote
You can set the PREG_OFFSET_CAPTURE flag to the preg_match_all to get the index. Anyway why you can't use create_function?
mck89
If you already have prepared functions for age and hobby instead of create_function write their name
mck89
A: 

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

MikeD