Solved

Regular expression not quite there, need help!

Posted on 2009-07-04
2
330 Views
Last Modified: 2012-05-07
Experts,

I'm trying to take out the content between the following tags in an RSS document:

<description>Content here </description>

My code is included below which outputs the matches however I get the following at the beginning of each line..

on>

so the line looks like

on> Some RSS content
on> Some more RSS content

So it's obviously not quite right.  Please help

on>
<?php
 
$pattern = '/<description>(.*?)<\/description>/';
 
preg_match_all($pattern, $buffer, $matches);
//print_r($regMatches);
 
foreach ($matches[0] as $val) {
	$val = substr($val, 10);
	echo "$val <br>";
}
 
?>

Open in new window

0
Comment
Question by:nickmarshall
2 Comments
 
LVL 13

Expert Comment

by:marchent
ID: 24777224
$pattern = '/<description>([^<]*)<\/description>/';
0
 
LVL 8

Accepted Solution

by:
fcardinaux earned 500 total points
ID: 24777227
The subpattern defined by your parentheses isn't in $machtes[0], but in $matches[1]:

<?php
 
$pattern = '/<description>(.*?)<\/description>/';
 
preg_match_all($pattern, $buffer, $matches);
//print_r($regMatches);
 
foreach ($matches[1] as $val) {
        echo "$val <br>";
}
 
?>

Open in new window

0

Featured Post

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question