Link to home
Start Free TrialLog in
Avatar of Alex Lord
Alex Lord

asked on

insert domain into src string in php

"<p><img alt="" src="uploaded_files/12/images/_gzyefj8rwhokhhzgprzltl72ejkfbmt4t8yenimkbvvk0ktmf0xjctabnaljim9.jpg" style="height:285px; width:285px" /></p>
"

Open in new window


this is my string in php, i need to add into it but i dont want to delete it, for example, i need to alter the url within the src, it is missing the the http//:Domain it suspose to be

https//:Domain.com/uploaded_files/12/images/_gzyefj8rwhokhhzgprzltl72ejkfbmt4t8yenimkbvvk0ktmf0xjctabnaljim9.jpg i need to insert domain in the back end.

im using php
Avatar of Zakaria Acharki
Zakaria Acharki
Flag of Morocco image

You could parse the HTML using `DOMDocument` class and perform all the changes you want :

$content = '<p><img alt="" src="uploaded_files/12/images/_gzyefj8rwhokhhzgprzltl72ejkfbmt4t8yenimkbvvk0ktmf0xjctabnaljim9.jpg" style="height:285px; width:285px" /></p>';
$html = new DOMDocument();
$html->loadXML($content);
$img = $html->getElementsByTagName('img')[0];
$img->setAttribute('src', 'http://example.com/' . $img->getAttribute('src'));
$content = $html->saveHTML();

echo $content;

Open in new window

Avatar of Alex Lord
Alex Lord

ASKER

   $html = new DOMDocument();
            $html->loadXML($_POST['quick_email']);
            $img = $html->getElementsByTagName('img')[0];
            $img->setAttribute('src', $_SERVER['HTTP_HOST'] . $img->getAttribute('src'));
            $content = $html->saveHTML(); 

Open in new window


my error

[12-Apr-2019 09:58:43 Europe/London] PHP Fatal error:  Uncaught Error: Call to a member function setAttribute() on null in /Applications/MAMP/htdocs/zymplify/client-area/controllers/email.ajax.php:1004
Stack trace:
#0 {main}
  thrown in /Applications/MAMP/htdocs/zymplify/client-area/controllers/email.ajax.php on line 1004
sorry im stupid lol my bad i got it now
No Problem, is it working now?
yes it is working, how i just need to implement a condition not to do these commands if no img is present.
ASKER CERTIFIED SOLUTION
Avatar of Zakaria Acharki
Zakaria Acharki
Flag of Morocco image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Zakaria Acharki

thank you for your help, great learning experience
Glad to help, welcome anytime.