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
Post a Comment