Jump to content

[SOLVED] trim and replace help on certain text


jesushax

Recommended Posts

ok so heres my example

<p>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Suspendisse purus enim, gravida et, dictum sed, cursus id, tellus
</p><p><br />
Nunc sodales consequat dui. Pellentesque faucibus ligula et dui. Suspendisse potenti. Vivamus sit amet ligula. Aenean pulvinar imperdiet enim. Nam aliquam purus. 
</p>

 

 

the paragrahs and breaks will be in different positions sometimes

 

but what i want to do is remove all <br /> from the submitted text

 

and remove the the <p> if the submitted text begins with one and remove the </p> if the submitted text ends with one

 

and only keep </p><p> if there is one

 

so

remove all <br />

remove first <p>

remove last </p>

keep </p><p>

 

cheerss

 

 

Link to comment
Share on other sites

I'm sure there's an easier way, but I like complex (call me a freak.....)

<?php
$code="Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Suspendisse purus enim, gravida et, dictum sed, cursus id, tellus
</p><p><br />
Nunc sodales consequat dui. Pellentesque faucibus ligula et dui. Suspendisse potenti. Vivamus sit amet ligula. Aenean pulvinar imperdiet enim. Nam aliquam purus. 
</p>";
$code = str_replace("<br />", "", $code);
$code_array = str_split($code);
$code_count = count($code_array);
$count = $code_count - 4;
$count_p = $count++;
if ($code_array[0] == "<" && ($code_array[1] == "p" || $code_array[1] == "P")){
unset($code_array[0]);
unset($code_array[1]);
unset($code_array[2]);
$code_count = count($code_array);
$count = $code_count - 1;
if ($code_array[$count] == "<" && $code_array[$count + 1] == "/"){
	unset($code_array[$code_count]);
	++$code_count;
	unset($code_array[$code_count]);
	$code_count++;
	unset($code_array[$code_count]);
	$code_count++;
	unset($code_array[$code_count]);
	array_pop($code_array);
}
$code = implode($code_array);
}

elseif ($code_array[$count - 1] == "<" && $code_array[$count] == "/"){
	array_pop($code_array);
	array_pop($code_array);
	array_pop($code_array);
	array_pop($code_array);
	$code = implode($code_array);
}
print $code;
?>

 

Link to comment
Share on other sites

<pre>
<?php
$data = <<<DATA
<p>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Suspendisse purus enim, gravida et, dictum sed, cursus id, tellus
</p><p><br />
Nunc sodales consequat dui. Pellentesque faucibus ligula et dui. Suspendisse potenti. Vivamus sit amet ligula. Aenean pulvinar imperdiet enim. Nam aliquam purus. 
</p>
DATA;

$data = str_replace('<br />', '', $data);
$data = preg_replace('%\A\s*<p>|</p>\s*\z%', '', $data);
echo htmlspecialchars($data);
?>
</pre>

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.