Link to home
Start Free TrialLog in
Avatar of Yuri Boyz
Yuri BoyzFlag for Uganda

asked on

how to read images from img tag from html content

I am using CMS to build HTML pages. All the html content is saved in Database. That HTML code contains text, formatted html and images etc.
Each image has <img> tag.

I am using Sphinx Full Text Search. I have make an index of the column which contains HTML of the page.

Here is my code


include 'C:\Sphinx\api\sphinxapi.php';

// Build search query
$cl = new SphinxClient();

$cl->SetServer( "localhost", 9312 );
$cl->SetMatchMode( SPH_MATCH_EXTENDED  );
$cl->SetRankingMode ( SPH_RANK_SPH04 );

$q  = '"' . $cl->EscapeString($_REQUEST['input']) . '"/1';
$searchresults = $cl->Query($q, 'my_index' );

if (!isset($searchresults["matches"]) )
{
	print("No Result Found");
	exit();
}


foreach($searchresults["matches"] as $match)
{

  $entry = array("title" => "");
  if(isset($match["attrs"]))
  {
        "<br>".$entry["title"] = (isset($match["attrs"]["title"]) ? $match["attrs"]["title"] : "");	
	$entity["entity_id"]  = (isset($match["attrs"]["entity_id"]) ? $match["attrs"]["entity_id"] : "");
	
						// Getting Link //
	$qry = "select * from url_alias where source like 'node/$entity[entity_id]'";
	$rst = mysqli_query($conn,$qry) or die(mysqli_error());
	$rst_fieds = mysqli_fetch_array($rst);
	$url_alias_link = $rst_fieds['alias'];
	   
	$entry["bundle"] = (isset($match["attrs"]["field_body_value"]) ? $match["attrs"]["field_body_value"] : "")."<br>";
	$entry["bundle2"] = strip_tags(html_entity_decode($entry["bundle"]));
	
		print "<div class='search-web'><a href='$protocol_val$prot$path_val$_REQUEST[lang_name]/$url_alias_link' target=_blank><h5 class='search-title color'>".$entry["title"]."</h5><span class='text hover-color'>".substr($entry["bundle2"], 0, 265)."...........<br><br></span></a></div>";
	
  }

Open in new window


How can i fetch image names from HTML content table/index.
Suppose I have 5 <img> tags in HTML content. If my search keyword matches with <img src="" > then its name will be saved in an array so that i will display
the image in my search result.

Hope you got some idea that what I want. If still any confusion then please send comments
Avatar of Steve Bink
Steve Bink
Flag of United States of America image

You can use preg_match_all() to discover <img> elements in your source.  Try this regex:
<img[^>]+src="([^">]+)"

Open in new window

Avatar of Yuri Boyz

ASKER

can you provide the more code in depth so that i can implement it?
You could do something like this:
$matches=[];
$pattern='/<img[^>]+src="([^">]+)"/i';
$found=preg_match_all ($pattern, $my_html, $matches);
if ($found) {
  // matches were found
  // the $matches array will hold the return
  // see [url="http://php.net/manual/en/function.preg-match-all.php"]http://php.net/manual/en/function.preg-match-all.php[/url]
  // for details on the array's structure
} else {
  // no matches were found
}

Open in new window


An example:
php > $my_html='<HTML>
php ' <HEAD>
php ' <TITLE>Your Title Here</TITLE>
php ' </HEAD>
php ' <BODY BGCOLOR="FFFFFF">
php ' <CENTER><IMG SRC="clouds.jpg" ALIGN="BOTTOM"> </CENTER>
php ' <HR>
php ' <a href="http://somegreatsite.com">Link Name</a>
php ' is a link to another nifty site
php ' <H1>This is a Header</H1>
php ' <H2>This is a Medium Header</H2>
php ' Send me mail at <a href="mailto:support@yourcompany.com">
php ' support@yourcompany.com</a>.
php ' <P> This is a new paragraph!
php ' <img src="some_pic.jpg">
php ' <P> <B>This is a new paragraph!</B>
php ' <BR> <B><I>This is a new sentence without a paragraph break, in bold italics.</I></B>
php ' <HR>
php ' </BODY>
php ' </HTML>
php ' ';
php > $pattern='/<img[^>]+src="([^">]+)"/i';
php > $matches=[];
php > $found=preg_match_all($pattern, $my_html, $matches);
php > var_export($found);
2
php > var_export($matches);
array (
  0 =>
  array (
    0 => '<IMG SRC="clouds.jpg"',
    1 => '<img src="some_pic.jpg"',
  ),
  1 =>
  array (
    0 => 'clouds.jpg',
    1 => 'some_pic.jpg',
  ),
)

Open in new window

LEt me check this and I will get back. Thanks
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.