Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Extracting tags from a string using regular expressions

Posted on 2011-02-17
2
Medium Priority
?
358 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
2 Comments
 
LVL 35

Accepted Solution

by:
Terry Woods earned 1000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

971 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