Jump to content

Removing only the first line break at the beginning of string


Texan78

Recommended Posts

Hello, I have this XML file I am parsing and I have hit a small formatting issue that I can't seem to get around. 

 

Here is what the the output of the RSS looks like. 

<description><![CDATA[<div style='text-align:left;'>Exit ramp closed. <br/><b>Current Status:</b> Open<br/><b>Affected Lanes:</b> Exit Ramp<br/><b>Dates:</b> Wednesday, May 28 - Thursday, May 29<br/><b>Days Closed:</b> <font color='#808080'>S</font><font color='#808080'>M</font><font color='#808080'>T</font><font color='#FF0000'><b>W</b></font><font color='#FF0000'><b>T</b></font><font color='#808080'>F</font><font color='#808080'>S</font>   20:00 PM - 6:00 AM</div>]]></description>

Notice at the first is "Exit ramp closed." There is nothing before it but, for some weird reason when I parse it. It is on a new line like so, notice it should be up there with "Incident" but, it is dropped down below it. 

Incident:
Exit ramp closed. 
Current Status: Open
Affected Lanes: Exit Ramp
Dates: Wednesday, May 28 - Thursday, May 29
Days Closed: SMTWTFS   20:00 PM - 6:00 AM

This is how it is formated in the td cell. 

<td style='{$td2Style}'><strong>Incident:</strong> {$incident_data_desc}</td>\n";

So as you can see above, there is nothing before it that would cause it to break to a new line both in the table or in the RSS. 

 

So how can I remove that first return only is that is on the same line with it and not below it. 

 

I have tried this and a couple of other things like trim with no luck. 

 

$description         = $item->description;
    $incident_data_desc  = str_replace("\r", '', $description );

Note, I only need that first one removed, the others are fine. I can do it but it will remove all the breaks and I only want to remove that first one that is causing the line to be on a new line. 

 

Any suggestions?

 

-Thanks

 

Link to comment
Share on other sites

It is caused by the div as Jacques1 pointed out. So to prevent the break between the Incident and Exit Ramp text you need to remove the <div></div> tags. Example

// strip the div tags from the data
$incident_data_desc = preg_replace('~^<div[^>]+>(.*?)</div>$~is', '$1', $incident_data_desc);
Edited by Ch0cu3r
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.