Search code examples
phparraysimageassociative-arrayimageurl

How to extract only the image path from the array I'm getting?


I'm using one CMS written in PHP. I'm making use of it's built-in functions to do the operations. At one place I want to get the path of an image for it following is the code which is working fine and returning the image.

$sUserProfileImage = Phpfox::getLib('image.helper')->display(array_merge(array('user' => Phpfox::getService('user')->getUserFields(true)), array (              
        'path' => 'core.url_user',
        'file' => Phpfox::getUserBy('user_image'),
        'suffix' => '_75_square',
        'max_width' => 52,
        'max_height' => 52
        )
    )
);

Now I tried to print the data from that array by using print_r($sUserProfileImage) function.

Then when I open this PHP file in a browser the image got displayed in proper dimension instead of printing the array content.

Then I viewed the HTML source of the page. There I got following details:

<a href="http://54.174.50.242/profile-244/" title="Campusknot ."><img src="http://54.174.50.242/file/pic/user/2015/02/52x52xade52764529ccd469bbddf98aa62712c_75_square.jpg,qt=54d2071f7ab6c.pagespeed.ic.8JdGurxQEJ.jpg" alt="Campusknot ." height="52" width="52" pagespeed_url_hash="2704114946"/></a><script pagespeed_no_defer="" type="text/javascript">//<![CDATA[
(function(){var e=encodeURIComponent,f=window,h=document,m="width",n="documentElement",p="height",q="length",r="prototype",s="body",t="&",u="&ci=",w="&n=",x="&rd=",y=",",z="?",A="Content-Type",B="Microsoft.XMLHTTP",C="Msxml2.XMLHTTP",D="POST",E="application/x-www-form-urlencoded",F="img",G="input",H="load",I="oh=",J="on",K="pagespeed_url_hash",L="url=",M=function(a,c,d){if(a.addEventListener)a.addEventListener(c,d,!1);else if(a.attachEvent)a.attachEvent(J+c,d);else{var b=a[J+c];a[J+c]=function(){d.call(this);b&&b.call(this)}}};f.pagespeed=f.pagespeed||{};var N=f.pagespeed,O=function(a,c,d,b,g){this.d=a;this.f=c;this.g=d;this.a=g;this.c={height:f.innerHeight||h[n].clientHeight||h[s].clientHeight,width:f.innerWidth||h[n].clientWidth||h[s].clientWidth};this.e=b;this.b={}};O[r].j=function(a){a=a.getBoundingClientRect();return{top:a.top+(void 0!==f.pageYOffset?f.pageYOffset:(h[n]||h[s].parentNode||h[s]).scrollTop),left:a.left+(void 0!==f.pageXOffset?f.pageXOffset:(h[n]||h[s].parentNode||h[s]).scrollLeft)}};O[r].i=function(a){if(0>=a.offsetWidth&&0>=a.offsetHeight)return!1;a=this.j(a);var c=a.top.toString()+y+a.left.toString();if(this.b.hasOwnProperty(c))return!1;this.b[c]=!0;return a.top<=this.c[p]&&a.left<=this.c[m]};O[r].l=function(){for(var a=[F,G],c=[],d={},b=0;b<a[q];++b)for(var g=h.getElementsByTagName(a[b]),k=0;k<g[q];++k){var v=g[k].getAttribute(K);v&&g[k].getBoundingClientRect&&this.i(g[k])&&!(v in d)&&(c.push(v),d[v]=!0)}b=!1;a=I+this.g;this.a&&(a+=w+this.a);if(0!=c[q]){a+=u+e(c[0]);for(b=1;b<c[q];++b){d=y+e(c[b]);if(131072<a[q]+d[q])break;a+=d}b=!0}this.e&&(d=x+e(JSON.stringify(this.h())),131072>=a[q]+d[q]&&(a+=d),b=!0);N.criticalImagesBeaconData=a;if(b){var c=this.d,b=this.f,l;if(f.XMLHttpRequest)l=new XMLHttpRequest;else if(f.ActiveXObject)try{l=new ActiveXObject(C)}catch(P){try{l=new ActiveXObject(B)}catch(Q){}}l&&(l.open(D,c+(-1==c.indexOf(z)?z:t)+L+e(b)),l.setRequestHeader(A,E),l.send(a))}};O[r].h=function(){for(var a={},c=h.getElementsByTagName(F),d=0;d<c[q];++d){var b=c[d],g=b.getAttribute(K);if("undefined"==typeof b.naturalWidth||"undefined"==typeof b.naturalHeight||"undefined"==typeof g)break;if("undefined"==typeof a[b.src]&&0<b[m]&&0<b[p]&&0<b.naturalWidth&&0<b.naturalHeight||"undefined"!=typeof a[b.src]&&b[m]>=a[b.src].n&&b[p]>=a[b.src].m)a[g]={renderedWidth:b[m],renderedHeight:b[p],originalWidth:b.naturalWidth,originalHeight:b.naturalHeight}}return a};N.k=function(a,c,d,b,g){var k=new O(a,c,d,b,g);M(f,H,function(){f.setTimeout(function(){k.l()},0)})};N.criticalImagesBeaconInit=N.k;})();pagespeed.criticalImagesBeaconInit('/mod_pagespeed_beacon','http://54.174.50.242/webservice/all_feeds.php','WyQ3C4mRrQ',false,'d8V9nFMe46w');
//]]></script>

From the above data I only wanted the below response data, the image URL :

<img src="http://54.174.50.242/file/pic/user/2015/02/52x52xade52764529ccd469bbddf98aa62712c_75_square.jpg,qt=54d2071f7ab6c.pagespeed.ic.8JdGurxQEJ.jpg" alt="Campusknot ." height="52" width="52" pagespeed_url_hash="2704114946"/>

I'll have to assign this image URL as a new element in another array but I'm not able to get it. And one more thing there is no other way than the code written above to get the image URL. So please help me in this regard.

Thanks in advance.


Solution

  • You can get image url from html using regex

    preg_match_all('/<img .*src=["|\']([^"|\']+)/i', $html, $matches);
    foreach ($matches[1] as $key=>$value) {
        echo $value."<br>";
    }