wordpress - Extract Image Sources from text in PHP - preg_match_all required -


i have little issue preg_match_all not running properly.

what want extract src parameter of all images in post_content wordpress string - not complete html document/dom (thus cannot use document parser function)

i using below code unfortunately untidy , works 1 image src, want image sources string

preg_match_all( '/src="([^"]*)"/', $search->post_content, $matches);  if ( isset( $matches ) ) {    foreach ($matches $match)  {  if(strpos($match[0], "src")!==false) { $res = explode("\"", $match[0]); echo $res[1]; }  }  } 

can please here...

using regular expressions parse html document can error prone. in case not img elements have src attribute (in fact, doesn’t need html attribute @ all). besides that, might possible attribute value not enclosed in double quote.

better use html dom parser php’s domdocument , methods:

$doc = new domdocument(); $doc->loadhtml($search->post_content); foreach ($doc->getelementsbytagname('img') $img) {     if ($img->hasattribute('src')) {         echo $img->getattribute('src');     } } 

Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -