On Wed, 8 Jan 2003, ELLIOTT,KATHERINE A (HP-FtCollins,ex1) wrote:

> OK, so I've gotten NO responses to my query below so I
> thought I'd ask for something slightly different and see what
> I get.

The follow-up inquery makes much more sense than the original ;)

> If I have a bunch of plain text data, how can I pull out the
> strings containing "http".  I've tried several different things
> but what I end up with is more or less than what I want.
> Once I have this, I can add the URL code and replace the
> string but I haven't figured out how to do this.

Say that the paragraph you gave is a string in a variable, $TEXT ...

$allowedChars = "/:@\w.#~?%+=&!-";
preg_match_all(
  "{\bhttp:[$allowedChars]+?(?=[$.:?\-]*[^$allowedChars]|$)}x",
  $TEXT,
  $URLS
);
foreach( $URLS[0] as $link ){
        print( "<a href=\"${link}\">${link}</a>\n" );
}

That should get ya started.  Read up on regular expressions, and the
regexp functions in PHP to make heads & tails out of the code, other than
"it works."  (So far it works well for my intentionally limited purposes
... I wouldn't be surprised that someone finds a case where my code
breaks, or someone can/does easily extend the regexp to grok more stuff.)

http://www.oreilly.com/catalog/regex2/
http://www.php.net/manual/en/ref.pcre.php
http://www.php.net/manual/en/ref.regex.php

        g.luck,
        ~Chris



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to