Jump to content

String parsing


mjahkoh

Recommended Posts

I got the below Div. I gather i got the following subtitles in the div (sometimes not all)

$subtitle = array('Postal Address:','Street:','Town :','Telephone:','Mobile:','Contact Person:','Country:','e-mail:','Website:');

 

It occurs that the title is enclosed in font tag and the title text is what follows after the closing </font> upto the next <br>;

Please help on retrieving The Title and the text.

 

Regards

 

<div align="left" id="tabledetails__"><font color="#4E779F" size="+1">Beverages Ltd</font><br>

        <font color="brown">Postal Address: </font>1604120

            <br>

    <font color="brown">Street: </font>Karatina Town Council

            <br>

    <font color="brown">Town :</font>Kampala

            <br>

    <font color="brown">Telephone: </font>+254 414 983456

                    <br>

    <font color="brown"> Country: </font>Kenya

   

    <br>

    <font color="brown">e-mail: </font><a href="mailto:[email protected]">[email protected]</a>

            </div>

Link to comment
https://forums.phpfreaks.com/topic/245950-string-parsing/
Share on other sites

You can use either a DOM parser or a Regex method. Regex method:

 

preg_match_all('#<font[^>]+>([^<]+).*?>(?:<a.*?>)?([^<]+)#', $div, $matches, PREG_SET_ORDER);

 

The matches array will look something like this:

 

Array
(
    [0] => Array
        (
            [0] => <font color="#4E779F" size="+1">Beverages Ltd</font><br>
        
            [1] => Beverages Ltd
            [2] => 
        
        )

    [1] => Array
        (
            [0] => <font color="brown">Postal Address: </font>1604120
            
            [1] => Postal Address: 
            [2] => 1604120
            
        )

    [2] => Array
        (
            [0] => <font color="brown">Street: </font>Karatina Town Council
            
            [1] => Street: 
            [2] => Karatina Town Council
            
        )

    [3] => Array
        (
            [0] => <font color="brown">Town :</font>Kampala
            
            [1] => Town :
            [2] => Kampala
            
        )

    [4] => Array
        (
            [0] => <font color="brown">Telephone: </font>+254 414 983456
                    
            [1] => Telephone: 
            [2] => +254 414 983456
                    
        )

    [5] => Array
        (
            [0] => <font color="brown"> Country: </font>Kenya
     
       
            [1] =>  Country: 
            [2] => Kenya
     
       
        )

    [6] => Array
        (
            [0] => <font color="brown">e-mail: </font><a href="mailto:[email protected]">[email protected]
            [1] => e-mail: 
            [2] => [email protected]
        )

)

Link to comment
https://forums.phpfreaks.com/topic/245950-string-parsing/#findComment-1263134
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.