66610

Fnd specific place in string and get data from it

Question:

I have string with multiple image tags in it. Like this

<img src="/files/028ou2p5g/blogs/9d66329f4/5844644f69fe7-64.jpg">

I want to find <strong>FIRST</strong> such tag, and get image name from it <strong>5844644f69fe7-64.jpg</strong> How can be this done in PHP asuming there is a lot of other text and tags in string ?

Answer1:

You should use like what @moopet suggested. This is the code, but please give credit to @moopet.

$str = '<img src="/files/028ou2p5g/blogs/9d66329f4/5844644f69fe7-64.jpg">'; $doc = new DOMDocument(); $doc->loadHTML($str); $first_img = $doc->getElementsByTagName("img")[0]; var_dump( basename($first_img->getAttribute('src')) );

Answer2:

Don't use regex for this. Use PHP's DOM parser or an alternative to extract the tags, then use PHP's <a href="http://php.net/manual/en/function.basename.php" rel="nofollow">basename()</a> function on the src element to extract the filename.

Answer3:

Use <a href="http://php.net/manual/en/function.preg-match-all.php" rel="nofollow">preg_match_all()</a> to find all occurences and then get the first one.

Example: <br />

<?php preg_match_all('/<\s*img[^<>]+?src\s*=\s*[\'\"][^<>\'\"]+?\/([^<>\'\"\/]\.jpg)/', $html, $matches, PREG_SET_ORDER); var_dump($matches[0]); ?>

Recommend

  • PHP using regular expression to parse query string
  • Facebook profile pic coming up as Http: on secure website expected Https:
  • MVC3 generic handler (.ashx) for Images resizing (Need clean URL)
  • PHP file_exists() anomaly
  • MVVM: Image Bind Source from FileOpenPicker
  • node.js POST request fails
  • Google OAuth: can't get refresh token with authorization code
  • PHP Listener Script for Paypal Webhooks
  • What Makes These Two Array Adds Different?
  • Install PHP intl extension on MacOS
  • Magento Fatal error: Maximum execution error solution, on WAMP
  • Bad request using file_get_contents for PUT request in PHP
  • MailKit: The IMAP server replied to the 'EXAMINE' command with a 'BAD' response
  • Refering to the class itself from within a class mehod in Objective C
  • Get data from AJAX - How to
  • ImageMagick, replace semi-transparent white with opaque white
  • Syntax for setting draggablecursor property in google maps api
  • Get one-time binding to work for ng-if
  • FB SDK and cURL: Unknown SSL protocol error in connection to graph.facebook.com:443
  • Volley JsonObjectRequest send headers in GET Request
  • Resize panoramic image to fixed size
  • MySQL WHERE-condition in procedure ignored
  • Importing jscolor library in angular 2
  • Upload files with Ajax and Jquery
  • Web-crawler for facebook in python
  • Do I've to free mysql result after storing it?
  • A cron job substitute?
  • json Serialization in asp
  • log4net write single file for each call to log.info
  • trying to dynamically update Highchart column chart but series undefined
  • How can I remove ASP.NET Designer.cs files?
  • python draw pie shapes with colour filled
  • Is there any way to bind data to data.frame by some index?
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Observable and ngFor in Angular 2
  • How can i traverse a binary tree from right to left in java?
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal