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:sales@karatina.co.ug">sales@karatina.co.ug</a>

            </div>

Link to comment
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:sales@karatina.co.ug">sales@karatina.co.ug
            [1] => e-mail: 
            [2] => sales@karatina.co.ug
        )

)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.