Forum Moderators: coopster

Message Too Old, No Replies

Preg Match without match boundries

         

itledi

1:23 am on Feb 7, 2008 (gmt 0)

10+ Year Member



Hello,

I'm trying to match a pattern using pre_match.

While my match works, I would like to exclude the outside of my match, leaving only the inbetween.

preg_match("/href=\".*\"/isx",$feed[$i],$link);

Current returns:

href=\"http://www.example.com/\"

What I would like to return:

http://www.example.com/

[edited by: eelixduppy at 1:46 am (utc) on Feb. 7, 2008]

GamingLoft

1:40 am on Feb 7, 2008 (gmt 0)

10+ Year Member



The whole function/script may help people who see this better understand how they can help...

Nevermind that, uhm thats quite easy to do... a simple way for me (the way i undestand) would be like this...

a file i had recently done but it can be modified to suit you're needs.


<?php
$artist = urlencode($_GET['artist']);
$url = 'http://www.last.fm/music/'.$artist.'/+wiki';
$file = file_get_contents($url);
$factbook2 ='<div class=\"f\"><span class=\"iesucks\">&nbsp;</span></div>
</div></div>';
$thatline = '<div style=\"margin-top:20px; border-top:1px solid #CCC; padding-top:10px;\">';
$factbook2 = stripslashes($factbook2);
$factpos2 = strpos($file, $factbook2);
$desc = substr_replace($file, ' ', 0, $factpos2) . '';
$desc = str_replace($factbook2, ' ', $desc);
$thatline = stripslashes($thatline);
$thatlinepos = strpos($desc, $thatline);
$desc = substr_replace($desc, '', $thatlinepos, -1) . "<br />\n";
echo $desc;
?>

[edited by: GamingLoft at 1:59 am (utc) on Feb. 7, 2008]

PHP_Chimp

1:27 pm on Feb 7, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



preg_match("/href=\".*\"/isx",$feed[$i],$link);

If you want to capture parts then you use ()'s around the area you want to capture.
So in your case I guess that you want -

preg_match("/href=\"[b]([/b].*[b])[/b]\"/isx",$feed[$i],$link);

Also remember that * means 0 or more, so dont you want to use + to force at least 1 character? As a blank href is not what you want to be matched, however your pattern will match href="".

As you also use brackets to group things together and you may not want them captured then you can use (?:


(?: group of stuff not to be captured )