Advertisement

07.10.2008 at 11:51AM PDT, ID: 23554996
[x]
Attachment Details

How do you remove parts of a string between angle brackets "<" or ">" using PHP?

Asked by schizek in PHP Scripting Language, PHP and Databases

Tags: , ,

I am trying to remove parts of a string that are between angle brackets.  Here is an example...

$myString = "The <strong>quick brown</strong> fox jumped over the <span style=\"color: red;\">lazy</span> dog.";

I would like it to read...

"The quick brown fox jumped over the lazy dog." (without any HTML formatting)

Thanks to EE, I was able to get it to work for text between parentheses.  However, it doesn't seem to work the same with the angle brackets.  I tried modifying the code and instead it removed everything between the very first and very last angle bracket (output was just... "The dog.")

Ideas?Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
//Using Parentheses
$myString = "The (quick brown) fox jumped over the (lazy) dog.";
echo "<p>" . $myString . "</p>";
$newString = preg_replace("/\([^)]*\) ?/", "", $myString);
echo "<p>" . $newString . "</p>";
//OUTPUT: The fox jumped over the dog.
 
//This is what I tried for the angle brackets
$myString = "The <strong>quick brown</strong> fox jumped over the <span style=\"color: red;\">lazy</span> dog.";
echo "<p>" . $myString . "</p>";
$newString = preg_replace("/<[^)]*> ?/", "", $myString);
echo "<p>" . $newString . "</p>";
//OUTPUT: The dog.
//DESIRED OUTPUT: The quick brown fox jumped over the lazy dog.
 
 
[+][-]07.10.2008 at 12:12PM PDT, ID: 21976492

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: PHP Scripting Language, PHP and Databases
Tags: PHP, RegEx, Regular Expressions
Sign Up Now!
Solution Provided By: ncoo
Participating Experts: 1
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_Related_20080208