This question already has answers here:
Batch script to replace PHP short open tags with <?php
(13 answers)
Closed 9 years ago.
I need to replace all PHP lines that are like <?=[something]?> with this: <?php echo [something]; ?> The problem is that there can be almost anything in that something clause even ? and < but they can't be adjacent. I am kinda new to regexes and wrote this very messy expression: \<\?\=([a-zA-Z0-9()=#<>\[\]\\/'"._$\?:, \-]*)([;]*)\?\> and replaced it with <?php echo \1; ?>.
It works but it can't match something like this:
<?=[something]?><tr><td><?=[something]?> when it's all in one line. It matches the whole line altogether.
Any help will be greatly appreciated.
This code should work:
$s = '<?=[something]?><tr><td><?=[something]?>';
$s = preg_replace('/(<\?)=\s*(\[[^]]*\])\s*(\?>)/', '$1php echo $2; $3', $s);
var_dump($s);
OUTPUT:
string(56) "<?php echo [something]; ?><tr><td><?php echo [something]; ?>"
Related
This question already has answers here:
Make text between asterisks bold
(3 answers)
Closed 5 years ago.
How can I take a string like this: *Up to* $1,000
and turn it into this: <span>Up to</span> $1,000
The stars can be anywhere in the string and also there can be multiple sets of stars. But each set should replaced with span's.
e.g.
text *test* here = text <span>test</span> here
text here *test* right *now* = text here <span>test</span> right <span>now</span>
I need to be able to pass the value into a function and receive the formatted string in return. Thanks ahead of time.
Simple regex can do this:
function replace_star($str) {
return preg_replace('~\*([^*]*)\*~ms', '<span>\1</span>', $str);
}
echo replace_star('*Up to* $1,000') . "\n";
echo replace_star('text here *test* right *now*');
Output:
<span>Up to</span> $1,000
text here <span>test</span> right <span>now</span>
This question already has answers here:
Remove all html tags from php string
(9 answers)
Regex str_replace
(2 answers)
Closed 5 years ago.
sorry for not beeing able to put the title exactly what my question, I can't find a way to ask it.
So im trying to replace all ' in a text inside and replace it for ' in php
I have a variable $desc = "he's such a good person and he'll be very successfull";
and I'm trying to do the following
$desc = str_replace("'","'",$desc);
But no success is there a way to use regex in str_replace?
Yes it looks to be fine now... for some reason.
Is there a way to use regex for it?
for example to remove html tags from the text?
$desc = "<strong> he's such a good person </strong> <br /> he'll be very successfull";
$desc = str_replace("<*>"," ",$desc);
You may try to use the correct PHP function to do this job, so please take a look at : preg_replace doc.
In your case, you would like to use like this :
preg_replace('/'/', "'", $desc);
Take a look at this execution:
https://eval.in/800948
Does it have to be a regex because this is a lot simpler way to do it
$desc = "he's such a good person and he'll be very successfull";
$new = str_replace(''', "'", $desc);
echo $new;
RESULT:
he's such a good person and he'll be very successfull
You don't need to use regex as it will be more complex
use
$desc = "he's such a good person and he'll be very successfull";
$desc = str_replace("'","'",$desc);
echo "after replace :"." ".$desc;
it is more simpler :)
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;
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);