0

Possible Duplicate:
How to parse and process HTML with PHP?

i have an array which contains all the scanned links from a regex format. but in the regex, for www.apple.com, it only has links such as /macbookair/ and /osx/, not http://www.apple.com/macbookair/. is there a way for me to set default values like 'http://www.apple.com/' before the values? that way they are working links. my regex is

 /<a(?:[^>]*)href=\"([^\"]*)\"(?:[^>]*)>(?:[^<]*)<\/a>/is

is it worth me correcting the above regex? or is there a way to set the default value for an array? i would rather people not mention to use 'html dom' or something like that because i would rather just use one of the ways i suggested.

Community
  • 1
  • 1
Noah Smith
  • 203
  • 4
  • 9
  • 3
    Welcome to Stack Overflow! Not really related, but still: Please refrain from parsing HTML with RegEx as it will [drive you į̷̷͚̤̤̖̱̦͍͗̒̈̅̄̎n̨͖͓̹͍͎͔͈̝̲͐ͪ͛̃̄͛ṣ̷̵̞̦ͤ̅̉̋ͪ͑͛ͥ͜a̷̘͖̮͔͎͛̇̏̒͆̆͘n͇͔̤̼͙̩͖̭ͤ͋̉͌͟eͥ͒͆ͧͨ̽͞҉̹͍̳̻͢](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454). Use an [HTML parser](http://stackoverflow.com/questions/292926/robust-mature-html-parser-for-php) instead. – Madara's Ghost Sep 08 '12 at 13:13
  • Loop over the array, and prepend desired string - it's a line of code. – moonwave99 Sep 08 '12 at 13:16
  • @PeeHaa: I disagree. The question here is about the URL, not the HTML. – Madara's Ghost Sep 08 '12 at 13:19
  • implode("http://www.apple.com/", $extracted_array); – FirmView Sep 08 '12 at 13:19
  • Hmmm what is that regex doing in there than :P @Truth (you may just be right though). "is it worth me correcting the above regex?" The question is just not clear in my defense :) – PeeHaa Sep 08 '12 at 13:21
  • would an implode statement do my first task? o.O – Noah Smith Sep 08 '12 at 13:22
  • what is your first task? From your question, i understand that you have an array containing some strings, these values has to be prepended with a string. implode would do that. – FirmView Sep 08 '12 at 13:40
  • Or As @moonwave99 suggestion, loop over the array and prepend it – FirmView Sep 08 '12 at 13:46

1 Answers1

0
<?php

$default_value = 'http://www.apple.com'; // without slash

foreach( $results as &$link )
{
    if( $link[0] == '/' )
    {
        $link = $default_value. $link;
    }
}
Draex_
  • 3,011
  • 4
  • 32
  • 50