Preg_match url from string in an array and return value

dthardy used Ask the Experts™
I am parsing xml into an array and trying to extract the url from a string variable containing html.  I want to create a variable with each url value and output the results.  I can't seem to get the output correct.

The string in the array with the html code is - linkCodeHTML
The variable I have for the extracted url is - linkURL

I have posted my code below.

    $developerKey = "00b9";
    $websiteId = "212";

    $ini = ini_set("soap.wsdl_cache_enabled","0");

    try {

        $client = new SoapClient("", array('trace'=> true));

        //Enter the request parameters for LinkSearch below.
		//For detailed usage of the parameter values, please refer to CJ Web Services online documentation

        $results = $client->searchLinks(array("developerKey" => $developerKey,
                                                "token" => '',
                                            "websiteId" => $websiteId,
                                        "advertiserIds" => 'joined',
                                             "keywords" => '',
                                             "category" => '',
                                             "linkType" => 'Text Link',
                                             "linkSize" => '',
                                             "language" => 'en',
                                      "serviceableArea" => 'US',
                                        "promotionType" => '',
                                   "promotionStartDate" => '',
                                     "promotionEndDate" => '',
                                               "sortBy" => '',
                                            "sortOrder" => 'asc',
                                              "startAt" => 0,
                                           "maxResults" => 100));

		//The loop below can be used to parse the results recieved in the response
        print "Your link search generated ".$results->out->totalResults." results\n";

		        echo "Displaying the first 10 results returned\n";
		        foreach ($results->out->links->LinkDetail as $linkDetail) {
		        preg_match ("/a[\s]+[^>]*?href[\s]?=[\s\"\']+".
                    , $linkURL);
		            echo "Advertiser Name: ".$linkDetail->advertiserName."\n";
		            echo "Category: ".$linkDetail->category."\n";
		            echo "Click Commission: ".$linkDetail->clickCommission."\n";
		            echo "Link Code HTML: ".$linkDetail->linkCodeHTML."\n";
		            echo "Link Code JavaScript: ".$linkDetail->linkCodeJavascript."\n";
		            echo "Link Description: ".$linkDetail->linkDescription."\n";
		            echo "Link Destination: ".$linkURL."\n";
		            echo "----------------------\n";
        echo "\n";

    } catch (Exception $e){
        echo "There was an error with your request or the service is unavailable.\n";
        print_r ($e);

Open in new window

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2011
Top Expert 2016

Instead of posting code that does not work, please post a sample of the input data and a sample showing what that input data would produce if the code did work.  Then we can help you with a working code sample.  Thanks, ~Ray


all of the code works with the exception of the variable I am trying to create (linkURL).  The preg_match works with a single string.  The problem I am having is the output of the array.  The variable outputs as 'array'.  You can see a sample of this script at:

An example of linkCodeHTML is "<a href="">Honda Goldwing Parts</a> - Genuine OEM parts"
Most Valuable Expert 2011
Top Expert 2016
OK, then go to line 44 in that script and add this:

echo "<pre>";

Then we can see what is inside that array!
Marco GasiFreelancer
Top Expert 2010
Keep in mind $linkUrl is an array: you can't get its value(s) qith an echo statement. You have to use var_dump to see them. But looking at your code, it seems like you have to change line 52 this way:

echo "Link Destination: ".$linkURL[0]."\n";



Thanks Ray Paseur and marqusG! Both of these helped provide the answer.  Var dump was a key.  Actual final variable turned out to be $linkURL[1][0]

For a short time you can see an example of the array output at -

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial