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>
Related
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 an answer here:
Convert keyboard emoticons into custom png and vice versa
(1 answer)
Closed 7 years ago.
Input - hey I'm smiling 😀
Output - hey I'm smiling <span class ="smile"></span>
Code
$emoticons = array('😀' =>'<span class ="smile"></span>') ;
$str = strtr($str, $emoticons) ;
I can't use str_replace because I have more than one element in $emoticons array.
This above code is not working the input and output remains same.
This works for me:
<?php
$str = "hey I'm smiling 😀 and I'm crying 😢 😢"; // input
$emoticons = array('😀' =>'<span class="smile"></span>','😢' =>'<span class="cry"></span>') ; // array of emoticons and spans
$output = strtr($str, $emoticons); // change emoticons from array to spans from array
echo $output; // print it
?>
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);
This question already has answers here:
What do 'lazy' and 'greedy' mean in the context of regular expressions?
(13 answers)
Closed 1 year ago.
Hi I hope someone can help, as not too hot on regular expressions.
Got a script snippet as follows..
<?php
$news="test message {image=abc} more text text text {image=def}";
$news=preg_replace_callback("/\{image=.*\}/i",function ($matches) { $field=$matches[0]; return "*".$field."*"; }, $news);
echo $news;
?>
However when i run it, it returns
test message *{image=abc} more text text text {image=def}*
Instead I want it to return..
test message *{image=abc}* more text text text *{image=def}*
Thanks for any help.
Make your regex non-greedy by using .*? instead of .*:
$news = "test message {image=abc} more text text text {image=def}";
$news = preg_replace_callback("/\{image=.*?\}/i",function ($matches) {
return "*".$matches[0]."*"; }, $news);
echo $news;
OUTPUT
test message *{image=abc}* more text text text *{image=def}*
Why callback?
$news = preg_replace("/(\{image=[^\}]+\})/i", "*$1*", $news);
This question already has answers here:
Check if strings are x% equal
(2 answers)
Closed 9 years ago.
There is any way for comparing strings and get result in following manner.
Text is Found eg. abc = abc
or Text is found but slightly different eg. abc = abd
or Text not found eg. abc = xyz
I thought of removing the html tags with strip_tags() and then calculate the similarity between the two strings with similar_text():
$text = 'Test paragraph';
$html = '<p>Test paragraph.</p><!-- Comment --> Other text';
$stripped_html = strip_tags($html); // Remove html tags
similar_text($stripped_html, $text, $percentage); // Calculating ...
echo $percentage;