We help IT Professionals succeed at work.
Get Started

Preg_match url from string in an array and return value

dthardy
dthardy asked
on
517 Views
Last Modified: 2012-05-11
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.
<?php

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

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

    try {

        $client = new SoapClient("https://linksearch.api.cj.com/wsdl/version2/linkSearchServiceV2.wsdl", 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\"\']+".
                    "(.*?)[\"\']+.*?>"."([^<]+|.*?)?<\/a>/",$linkDetail->linkCodeHTML
                    , $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

Comment
Watch Question
Most Valuable Expert 2011
Author of the Year 2014
Commented:
This problem has been solved!
Unlock 2 Answers and 5 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE