1

I have some script that extracts some text from a table and dumps it into a text file for me. I would like your help to strip out just the URL so that the text file just shows the URL.

{
$result = @mysql_query("SELECT post_content_filtered FROM wp_posts ORDER BY post_date desc limit 200");
$tsv  = array();
$html = array();
while($row = mysql_fetch_array($result, MYSQL_NUM)){
    $tsv[]  = implode("\t", $row);
    $html[] = "<tr><td>" .implode("</td><td>", $row) ."</td></tr>";
}

$tsv = implode("\r\n", $tsv);
$html = "<table>" . implode("\r\n", $html) . "</table>";
$fileName = 'finishedflippa.txt';
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=$fileName");
echo $tsv;

post_content_filtered text looks something like this:

blahblahblah http://www.example.com blahblahblah
blahblahblah http://www.12345.com blahblahblah
blahblahblah http://www.gfds.com blahblahblah
blahblahblah http://www.45tyhju.com blahblahblah

The blahblahblah is the same for each row. Thanks very much.

2
  • 1
    Next time please post a readable code yourself. Thank you. Commented Oct 12, 2012 at 13:10
  • Sorry! I had a lot of trouble even getting that far. Commented Oct 12, 2012 at 13:23

1 Answer 1

6

URL have very complex definition

You can try this simple match with preg_match_all

$str = 'text looks something like this: 
blahblahblah http://www.example.com blahblahblah 
blahblahblah http://www.12345.com blahblahblah 
blahblahblah http://www.gfds.com blahblahblah 
blahblahblah http://www.45tyhju.com blahblahblah ' ;

preg_match_all('!https?://[\S]+!', $str, $matches);
var_dump($matches[0]);
Sign up to request clarification or add additional context in comments.

1 Comment

This solution is limited to http(s) URLs, while there are many other valid forms of URLs. As OP indicated that theblahblahblah prefix is known and fixed - a better approach would be to filter on the prefix (e.g. !(?:blahblahblah )([\S]+)!), where ([\S]) should probably be replaced with an expression for a valid URL (which can be quite complex as noted).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.