Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parsing a link from text body

Status
Not open for further replies.

pdbowling

Programmer
Mar 28, 2003
267
US
Hello, All.

If I have a text block string from the body of an email and I want to parse links out of the text, how would I go about that?

I could search for href=" and take everything up to the next quote but how do I designate the area between the quotes as the text to pull into a new string?

Being a c# developer I tend to think in terms of positions withing the string.
Code:
newString = bodyString.subString(firstQuotePosition, secondQuotePosition)
but I am so unfamiliar with php that I don't know the syntax for something lik 'indexof(searchString)'.

Suggestions?

Thanks
PB

Gather or post content for free.
 

This would work like indexof(). Just call it twice and set the offset to the first value returned plus 1. For example:

PHP:
$intFirst = stripos($string, "href=\"");
$intSecond = stripos($string, "\"", $intFirst+1);

$length = $intSecond - intFirst;
$start = $intFirst + 6;
$link = substr($string, $start, $length);
 
That being said, you can also do this with regular expressions and it will probably work better. However, I do not know them as well as others on this board and hesitate to try and tell anyone how to use them.

Also, I didn't test that code before posting it, so there may be some errors in it.
 
something like this should work
Code:
function getLink($text){
 $pattern = '/href=("|\')(.*?)$1/i';
 $matches = preg_match_all($pattern, $text, $matches);
 return $matches;
}


 
Here's a tested version, but jpadie's regex should still work better.

PHP:
<?php

$fp = fopen("test.txt", 'rb');

if ($fp) {
  while (!feof($fp)) {
    $intFirst = $intSecond = $offset = 0;
    $string = fgets($fp);
  
    $intFirst = stripos($string, 'href="');
    if ($intFirst !== false) {
      $offset = $intFirst + 6;
      $intSecond = stripos($string, '"', $offset);
     
      $length = $intSecond - $offset;
      $start = $intFirst + 6;
      $link = substr($string, $start, $length);
      
      echo $link . "\n\n";
    }
  }
}
?>

test.txt was some HTML source from an internal page.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top