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.

10
  • Your requirement is not clear. Can you elaborate your requirement. Commented Jan 23, 2016 at 11:30
  • i want to get src data from <img> tag. Commented Jan 23, 2016 at 11:37
  • Parse html with some xml-parsers. Commented Jan 23, 2016 at 11:38
  • 1
    stackoverflow.com/questions/3404433/… try this Commented Jan 23, 2016 at 11:44
  • Get your data using $("#img1").src.split(",")[1]; and then send it using whatever mechanism in the links above Commented Jan 23, 2016 at 11:45

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

The question says PHP, not JavaScript
yes now it working , now if any one wants in php then convert it , don't do down votes , he try to help atleast
@pratikwebdev thanks for your kind help this also helps me
@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 :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.