This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 2 years ago.
Trying to remove only gif image from the content , tried below but that does remove all images
$content = "this is <img src="https://ecwowocc.com/test.gif"/> something with an <img src=https://ecwowocc.com/test.png"/> in it.";
$content = preg_replace("/<img[^>]+\>/i", "(image) ", $content);
echo $content;
i want to remove only GIF from the content
You can try with:
<img[^>]+src=\\?(?:"|')[^.]+\.gif\\?(?:"|')[^>]+\>
specifics:
will match src attribute either used with " or '
will match if " or ' is escaped or not
small caution: this regex will not support a > character in another attribute of the img tag
see it in Regex101
As suggested by AmigoJack, it can be improved to the form:
<img[^>]+src=(\\?["'])[^'"]+\.gif\1[^>]*\/?>
Try Using str_replace() function
Syntax,
str_replace(search, replace, subject);
<?php
$content = "this is <img src=\"test.gif\"/> something with an <img src=\"test.png\"/> in it.";
echo str_replace(".gif", ".png", $content);
?>
Related
This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 3 years ago.
As title, If I have some html <p><span style="font-style:italic">abcde</span><span style="font-weight:bold">abcde</span></p>, I want to strip the style tags and transform them into html tags, so to make it become <p><i>abcde</i><b>abcde</b></p>. How can I do that in PHP?
I notice that when I open the html in CKEditor, this kind of transformation is done automatically. But I want to do it in backend PHP. Thanks.
$string = '<p><span style="font-style-italic;font-weight:bold">abcde</span><span style="font-weight:bold">abcde</span></p>';
$dom = new DOMDocument();
$dom->loadHTML($string);
$xp = new DOMXPath($dom);
$str = '';
$results = $xp->query('//span');
if($results->length>0){
foreach($results as $result){
$style = $result->getAttribute("style");
$style_arr = explode(";",$style);
$style_template = '%s';
if(count($style_arr)>0){
foreach($style_arr as $style_item){
if($style_item == 'font-style-italic'){
$style_template = '<i>'.$style_template.'</i>';
}
if($style_item == 'font-weight:bold'){
$style_template = '<b>'.$style_template.'</b>';
}
}
}
$str .= sprintf($style_template,$result->nodeValue);
}
}
$str = '<p>'.$str.'</p>';
You can also use html tags under php parameters or php opening and closing tags like this
<?php
echo"<h1>Here is Heading h1 </h1>";
?>
Or you can Put your html code in " " after echo
Like this
<?php
echo"Your Html Code Here";
?>
$output = preg_replace('/(<[^>]+) style=".*?"/i', '$1', $input);
Match a < follow by one or more and not > until space came and the style="anything" reached. The /i will work with capital STYLE and $1 will leave the tag as it is, if the tag does not include style="". And for the single quote style='' use this:
(<[^>]+) style=("|').*?("|')
This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 3 years ago.
The following is automatically output via editor:
1001-web-file
I would like to add an ID:
<a id="replace" href="https://someurl.com/1001-web-file.pdf">1001-web-file</a>
Then use str_replace and a regular expression to find the anchors in question and replace with:
<img src="https://someurl.com/1001-web-file-pdf-290x300.jpg"/>
What I've managed to do is:
$replace = array(
'<a id="pdfthumb" href="' => '<img src="',
'.pdf">pdfthumb</a>' => '-pdf-290x300.jpg"/></br>'
);
$text = str_replace(array_keys($replace), $replace, $text);
return $text;
This works to tear down the anchor tag and rebuild as an img. But I can't do much more. I played around with some regex to create a wildcard and realized I need to create a variable for the href to use when I rebuild the HTML, but I'm stuck.
Any insight is much appreciated :)
In your case, I think it should be easier if you do it on client side using javascript.
My background is not PHP but you can use the same pattern to test with your PHP code:
Input:
1001-web-file
Output:
<img src="https://someurl.com/1001-web-file-pdf-290x300.jpg"/>
var input = '1001-web-file';
var pattern = /href=\"(.+)\.pdf\"/;
var match = input.match(pattern)[1];
input = input.replace(/(<a.*>).*(<\/a>)/, '$1<img src="' + match + '-pdf-290x300.jpg">$2');
console.log(input)
Here is translated php code of Tan javascript answer
$input = '1001-web-file';
preg_match('/href="(.+)\.pdf"/', $input, $m);
$match = $m[1];
$input = preg_replace('/(<a.*>).*(<\/a>)/', '$1<img src="'. $match .'-pdf-290x300.jpg">$2', $input);
echo $input;
This question already has answers here:
Fastest way to retrieve a <title> in PHP
(7 answers)
Closed 3 years ago.
<?php
$content=file_get_contents('example.com');
// it would return html <head>.....
<title>Example.com</title>
I want to extract example.com from title
$title=pick('<title>','</title>',$content);
Echo $title;
And it would show Example.com
You can use substr to substring the HTML content and stripos to find the title tags.
I add 7 to the position to remove the tag.
$html = file_get_contents('example.com');
$pos = stripos($html, "<title>")+7;
echo substr($html, $pos, stripos($html, "</title>")-$pos);
Example:
https://3v4l.org/qvC40
This assumes there is only one title tag on the page, if there is more then it will get the first title tag.
You can use file_get_content() instead of $string.
$string = "<title>MY TITLE</title>";
$pattern = "/<title>(.*?)<\/title>/";
preg_match($pattern, $string, $matches);
echo "RESULT : ".$matches[1];
Try using PHP's simple xml parser to read the title node.
$xml = simplexml_load_string(file_get_contents('example.com'));
echo $xml->head->title;
This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 6 years ago.
I have the following string in PHP:
$string = "<img src=\"url\" >HELLO WORLD<ol>I must replace the text after img and before ol.</ol>";
print htmlentities($string);
I want to find the substring HELLO WORLD (or whatever substring is, that is just an example of a text that will be completely dynamical), using the delimiters : "<img ... >" and "<ol>" and add <h3> delimiters. So the string above would result in:
<img src="url" ><h3>HELLO WORLD</h3><ol>I must replace the text after img and before ol.</ol>
I have tried the following code, of course with no success:
$string = preg_replace("/\<img (.*?)\> (.*?)\<ol\>/", "<img (.*?)><h3> (.*?)</h3></ol>", $string);
I know how to make very easy substituions, but the above condition is very far from my understanding.
I have found the answer by trial-and-error:
$string = "<img src=\"url\" >HELLO WORLD<ol>I must find the text after img and before ol.</ol>";
$string2 = preg_replace("/<img (.*?)>(.*?)<ol>/", "<img $1><h3>$2</h3><ol>", $string);
print htmlentities($string) . " <br />" . htmlentities($string2);
Explanation: I add the delimiters, and use $1 and $2 for matching the results between the delimiters in the right order.
This question already has answers here:
how to replace hyphen with blank space / white space? php
(2 answers)
Closed 8 years ago.
So, I have a piece of code I am using in most of my pages which sets the page title as the file name. This is because I don't want to have to change the title each time I create a new page.
This all works fine, except I don't want to have spaces (or rather, %20) being shown in the url when the file name contains spaces. I'd much rather have hyphens in place of the spaces, as it looks cleaner to the user. However, this means PHP will set the page title also with hyphens instead of spaces, which, frankly, looks ugly.
Is there a way I can rewrite this code to replace the hyphens with spaces?
The code:
<?php
echo '<title>';
$path_parts = pathinfo(__FILE__);
echo ucfirst($path_parts['filename']);
echo " - Tom's basic Web Tutz";
echo '</title>';
?>
You can use str_replace to replace the - with a space.
<?php
echo '<title>';
$path_parts = pathinfo(__FILE__);
echo ucfirst(str_replace('-', ' ', $path_parts['filename']));
echo " - Tom's basic Web Tutz";
echo '</title>';
?>
You can use implode adn explode for this
$string = "the string";
$arr = explode("string to replace",$string);
$string = implode("string to add ",$arr);
echo $string;