1

i have following html content

<p>Hello World<p>
<img style="height:175px; width:312px" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABVYAAAMACAIAAABAXKuVAAAAA3NCSVQICAjb4U/gAAAgAElEQVR4Xu....">
<span style="color:rgb(0, 0, 0)">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse fringilla ipsum vitae fermentum accumsan. Nam pellentesque</span>

Now i want to get text iVBORw0KGgoAAAANSUhEU.... contain into <img> tag and from src.

How can i get this specific text from html.

Dhara
  • 1,431
  • 4
  • 29
  • 47
  • Your requirement is not clear. Can you elaborate your requirement. – Deepak Nirala Jan 23 '16 at 11:30
  • i want to get `src` data from tag. – Dhara Jan 23 '16 at 11:37
  • Parse html with some xml-parsers. – u_mulder Jan 23 '16 at 11:38
  • 1
    http://stackoverflow.com/questions/3404433/get-content-within-a-html-tag-using-php-and-replace-it-after-processing try this – Amitesh Kumar Jan 23 '16 at 11:44
  • Get your data using `$("#img1").src.split(",")[1];` and then send it using whatever mechanism in the links above – Satej S Jan 23 '16 at 11:45
  • @SatejS — That gives *PHP Parse error: syntax error, unexpected '(', expecting variable (T_VARIABLE) or '$'* – Quentin Jan 23 '16 at 11:46
  • Do that in Javascript!Also, give your image an id img1 – Satej S Jan 23 '16 at 11:47
  • @SatejS — The question is tagged **PHP** not JavaScript. – Quentin Jan 23 '16 at 11:48
  • Correct, my method suggests sending the value from HTML to PHP via JS.That is, using JS to extract the exact data item required, and then sending it via POST/anything etc. – Satej S Jan 23 '16 at 11:48
  • @SatejS — So rather than just parse the HTML with PHP, they should grab all of the HTML, modify it to add JavaScript, send it to a browser, get the browser to extract the data, then send it back to the PHP (presumably by making an HTTP request and triggering a second PHP script) … this sounds like a massively overcomplicated approach to the problem. – Quentin Jan 23 '16 at 11:50

1 Answers1

3

Assign an id (test) to image element and with jquery you could fetch src attribute of image with below code.

Jquery

var srcImage = $("#test").attr("src");

Or with Javascript

var srcImage = document.getElementById("test").getAttribute("src");

Now you could perform string operation on srcImage if you want particular part of src

JSFiddle https://jsfiddle.net/aq0a9jqs/

Sorry Jazz I did not notice that it was exclusively for php. Kindly refer to question tagged by Quentin as reference. It provides multiple options on how to load/parse html content in php.

There are multiple ways to parse html from string/file/url. Once parsing is performed you could fetch elements from parsed content with supported functions. After referring to answers in tagged question I found SimpleHTMLDOMParser easy to use.

You could you it as below for your problem in particular.

Load HTML from file/URL

$html = file_get_html('http://www.example.com/'); //Your HTML url or file 

Find image with attribute id=test

$ret = $html->find('img[id=test]'); //this will work if image element has ID if you can not change html then use foreach method mentioned in documentations.

Get a attribute ( If the attribute is non-value attribute (eg. checked, selected...), it will returns true or false)

$value = $ret->src;

This is what I understood by referring to documentations here. I have not tested it. http://simplehtmldom.sourceforge.net/ and http://simplehtmldom.sourceforge.net/manual.htm#section_find

Give it a try and see how it works out. Also refer to other possibilities from tagged question and choose whichever is easy for you. I hope it helps you. lmk.

pratikpawar
  • 2,038
  • 2
  • 16
  • 20
  • The question says **PHP**, not JavaScript – Quentin Jan 23 '16 at 11:43
  • yes now it working , now if any one wants in php then convert it , don't do down votes , he try to help atleast – Amitesh Kumar Jan 23 '16 at 11:45
  • @pratikwebdev thanks for your kind help this also helps me – Dhara Jan 23 '16 at 11:49
  • @Jazz you are welcome. I updated answer with solution from my understanding of simplehtmlparser. Please try and check if it works for you. AmiteshKumar Thank you for consideration. I got to learn few things about php :) – pratikpawar Jan 23 '16 at 12:33