/    Sign up×
Community /Pin to ProfileBookmark

Multiple <br />’s?

This is strange… I have searched many places on the net and on these forums and I can’t find an answer to what I thought would be a simple question. Maybe I’m not searching for the right thing… I don’t know. Anyway, how can I contract many <br />’s into one? Even if they are on multiple lines? It can’t be a set number. It has to be any number of <br />’s. So like if I had 1000 <br />’s it would contract them into one <br />. Even if those <br />’s were on multiple lines. Thanks for the help. ?

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@rpanningNov 15.2004 — I think REGEX would do the trick however I'm not a REGEX pro. It should start off with something like this.
[code=php]$string = preg_replace("/(\<br \/\>){2,}/ig", '<br />', $string);[/code]
Copy linkTweet thisAlerts:
@JickauthorNov 15.2004 — Well I gave that a try and it didn't work... ?
Copy linkTweet thisAlerts:
@JonaNov 15.2004 — [font=trebuchet ms]Try the following. (I haven't tested it, but it should work with both XHTML and HTML.)[/font]

[font=courier new]

$str = preg_replace('/(<br( /)?>)+/i', '<br$2>', $str);

[/font]

[font=trebuchet ms][i]Edit: I couldn't get it to format correctly in PHP [b]or[/b] CODE tags.[/i][/font]
Copy linkTweet thisAlerts:
@JickauthorNov 15.2004 — Thanks Jona! That worked great! ?

One last question. Do you know how I could set it up so that it is applied to everything except any instances of [c][/c] in the string? So like if I had this:


------------------------------------
hello<br />

<br />

<br />

[c]<?php<br />

<br />

<br />

$hello = "cool";<br />

<br />

?>[/c]<br />

<br />

some more text that is not code.
------------------------------------



So the output would be:


------------------------------------
hello<br />

[c]<?php<br />

<br />

<br />

$hello = "cool";<br />

<br />

?>[/c]<br />

some more text that is not code.
------------------------------------



So basically it is contracting the br's outside the [c][/c] tags but not inside. Can't you do that with a regex? There could be more then one instance of the [c][/c] tags in the string so it would need to like ignore anything between [c] and [/c] and time it finds it in the string. Thanks for the help. ?
Copy linkTweet thisAlerts:
@JonaNov 16.2004 — [font=trebuchet ms]I'd suggest a line-by-line loop. Something like the following...[/font]

<i>
</i>&lt;?php
$data = $row['post']; # for example, from a MySQL database query
$data = explode("n", $data);
$inStr = false;
for($j=0; $j&lt;count($data); $j++){
if(stristr($data[$j], "[c]")){
$inStr = true;
}
if(stristr($data[$j], "[/c]")){
$inStr = false;
}
if(!$inStr){
$data[$j] = preg_replace('/(&lt;br( /)?&gt;)+/i', '&lt;br$2&gt;', $data[$j]);
}
$inStr = false;
}
$data = implode("n", $data);
?&gt;


[font=trebuchet ms]The forum automatically puts a space after the "?>" in the Regular Expression - use the one I gave you in my previous post to be sure it works.[/font]
×

Success!

Help @Jick spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...