Replace pattern between several string delimiters in PHP - Use of backreferences [duplicate] - php

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.

Related

remove gif image only from content [duplicate]

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);
?>

Rewriting HTML anchor using str_replace and regex [duplicate]

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;

PHP replace hyphen with space [duplicate]

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;

get word after a tag closing using regex? [duplicate]

This question already has an answer here:
XPath - select text after certain node
(1 answer)
Closed 9 years ago.
I have following string
<strong>Test: </strong> BD-F5300
I am interested in getting number BD-F5300. Number could be of any thing text,number.
Any help, how can I get it? Thanks.
You could make use of preg_replace
<?php
$str='<strong>Test: </strong> BD-F5300';
echo $str = preg_replace("~<(/)?strong>(.*?)<(/)?strong>~","", $str);
OUTPUT :
BD-F5300
do like this in JavaScript:
var src = "<strong>Test: </strong> BD-F5300";
var reg = /.*<\/.*>\s*([a-zA-Z0-9-]+)/g;
var group = reg.exec(src);
console.log(group[1]+'\r\n'); //group[1] is what you want !
If all you need is to get some content after </strong> then you can just use:
preg_match('#</strong> (.+)#', $string, $matches);
The desired match will be in $matches[1]. However, this requires that the <strong> tag and the text content you want to find are both on the same line. If there are multiples of these you want to match, you may want to use preg_match_all
If there is always a space before the beginning of the final text you want and if there are never any spaces in the actual number text you want, you can avoid regex by doing this:
$str = '<strong>Test: </strong> BD-F5300';
$solution = substr($str, strrpos($str, ' ') + 1);
var_dump($solution);

Removing excess ">" in string in PHP [duplicate]

This question already has answers here:
Remove style attribute from HTML tags
(9 answers)
Closed 9 years ago.
In my code I get a string which have html tags like so:
$string = '<div style="width:100px;">ABC 1234 <span> Test string, testing this string</span></div>';
Now, I removed the style attribute from the said string using preg_replace:
$string = preg_replace('/(<[^>]+) style=".*?"/i', '', $string);
After removing the style tag, I managed to remove the style attribute so the div tag ended up looking like <div>. The problem, I encountered after doing this is that I now get an excess > after the closing tag for the span so the string looks like this now:
$string = '<div>ABC 1234 <span> Test string, testing this string</span> > </div>';
My question is, why did I suddenly get an exccess >? Is there a different regular expression I can use that will get rid of the style attribute without the additional > appearing? Or is there any way I can get ride of this?
I tried using str_replace twice like so:
$string = str_replace("\n", "", $string);
$string = str_replace(">>", ">", $string);
But that did not work either.
I am not trying to remove the HTML tags, just the style part.
Use it only for this string.
<?php
$string = "<div style=\"width:100px;\">ABC 1234 <span> Test string, testing this string</span></div>";
$string = strip_tags($string,"<span>");
$string = "<div>".$string."</div>";
?>
Now the string is:
<div>ABC 1234 <span> Test string, testing this string</span></div>
I used This
$string = '<div style="width:100px;">ABC 1234 <span> Test string, testing this string</span></div>';
$output = preg_replace('/(<[^>]+) style=".*?"/i', '$1', $string);
die(htmlentities($output))
and the output is
<div>ABC 1234 <span> Test string, testing this string</span></div>
as you need

Categories