Forum Moderators: coopster

Message Too Old, No Replies

Trimming Text in a String

         

Komodo_Tale

4:44 am on Jun 11, 2006 (gmt 0)

10+ Year Member



Another dumb question. . .

I need to isolate urls from strings. I'm trying to remove everything before 'http:' and remove the '</a>' plus everything after that.

Thanks

Habtom

12:46 pm on Jun 11, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Something like the following can give you an idea.
$beg = strpos($yourtext, "http");
$las = strpos($h, "</a>");
$yoururl = substr($yourtext, $beg, $las);

You can even use preg_match, better.

Hab

eelixduppy

1:20 pm on Jun 11, 2006 (gmt 0)



I would suggest changing the following line:
$las = strpos($h, "</a>");
To:
$las = strpos($yourtext, "</a>");

Assuming you have the string stored in the variable $yourtext

Good luck!

Komodo_Tale

6:31 pm on Jun 11, 2006 (gmt 0)

10+ Year Member



Here is were I am at right now, getting a parse error:

/*
Where $listing==
<a title="A bug in a public software release that is so embarrassing that the author notionally wears a brown paper bag over his head for a while so he won&amp;#39;t be ..." href=http://www.example.org/jargon/html/B/brown-paper-bag-bug.html><b>brown</b>-<b>paper</b>-<b>bag</b> bug</a>
*/

function pc_link_extractor($s) {
$a = array() ;
if (preg_match_all('/<a\s+.*?href=[\"\']?([^\"\' >]*)[\"\']?[^>]*>(.&?)<\/a>i',
$s,$matches,PREG_SET_ORDER)) {
foreach($matches as $match) {
array_puch($a, array($match[1]m$match[2]));
}
}
return $a;
}

$links = pc_link_extractor($listing[1])
print_r($a) ;

[1][edited by: coopster at 8:49 pm (utc) on June 11, 2006]
[edit reason] generalized url [/edit]

Komodo_Tale

7:04 pm on Jun 11, 2006 (gmt 0)

10+ Year Member



a-ha

function pc_link_extractor($s) {
$a = array();
if (preg_match_all('/<a\s+.*?href=[\"\']?([^\"\' >]*)[\"\']?[^>]*>(.*?)<\/a>/i',
$s,$matches,PREG_SET_ORDER)) {
foreach($matches as $match) {
array_push($a,array($match[1],$match[2]));
}
}
return $a;
}