Solved

Extracting tags from a string using regular expressions

Posted on 2011-02-17
2
337 Views
Last Modified: 2012-08-13
Hello experts,

I have a string like....

<this>my name is <name>jonny</name><br/>i am <age>26</age></this>

I am trying to output an array like this...

[0]=<this>
[1]=my name is
[2]=<name>
[3]=jonny
[4]=</name>
[5]=<br/>
[6]=i am
[7]=<age>
[8]=26
[9]=</age>
[10]=</this>

So each tag is in a new array element and the plain text is in its own array element

i have been messing around with regular expressions to try to extract it correctly but am just getting angry...

Does anyone have any ideas?
Thanks
0
Comment
Question by:handypam
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 35

Accepted Solution

by:
Terry Woods earned 250 total points
ID: 34920382
$sourcestring="your source string";
preg_match_all('/<[^>]*>|[^<>]+/',$sourcestring,$matches);
echo "<pre>".print_r($matches,true);

Result:
    [0] => Array
        (
            [0] => <this>
            [1] => my name is
            [2] => <name>
            [3] => jonny
            [4] => </name>
            [5] => <br/>
            [6] => i am
            [7] => <age>
            [8] => 26
            [9] => </age>
            [10] => </this>
        )
0
 

Author Comment

by:handypam
ID: 34920400
PERFECT THANKS!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Building a glossary into your website 9 60
Failed to open stream: Permission denied in /var/www/html/export.php on line 24 5 78
Secure registration PHP 7 43
sitemap.xml 3 32
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question