Search percent matching text in html text [duplicate] - php

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;

Related

Convert *text* into <span>text</span> with function [duplicate]

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>

Convert emoji into custom images [duplicate]

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

Remove first <p> tag and replace the second <p> tag with <br /> [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 7 years ago.
I need to Remove first < p> tag and replace the second tag with < /br>
For example :
I have a string '< p>Lorem Ipsum< /p>< p>is simply dummy< /p>'
I need the result as
Lorem Ipsum< br />is simply dummy
Maybe something like this?
$input = '<p>Lorem Ipsum</p><p>is simply dummy</p>';
$output = nl2br(trim(str_replace(array("<p>","</p>"),array("","\n"),$input)));
Example: http://3v4l.org/Kmhkh

Regex exclude character between string tag [duplicate]

This question already has answers here:
Find an element by id and replace its contents with php
(3 answers)
Closed 8 years ago.
So i have a string that i want to search through using regex, and not any other method like domDocument etc.
Example:
<div class="form-item form-type-textarea form-item-answer2">
<div class="form-textarea-wrapper resizable"><textarea id="edit-answer2" name="answer2" cols="60" rows="5" class="form-textarea">
this is some text
</textarea>
</div>
</div>
Desired:
this is some text
So what i want to do from this is using 1 regex line be left with 'this is some text', which is not fixed and will be dynamic. I will then pass this through a preg_replace to get desired outcome.
Current regex is
div class="form-item.*class="form-textarea">$\A<\/textarea>.*<\/div>/gU
I have tried using the end of string and start of string anchors, but to no avail.
Don't parse HTML with regexes. Use a DOM parser:
$doc = new DOMDocument();
$doc->loadHTML($html);
$textarea = $doc->getElementById("edit-answer2");
echo $textarea->nodeValue;
if you want to modify the value:
$textarea->nodeValue = "foo bar";
$html = $doc->saveHTML();
Your regex would be,
/<textarea id[^>]*>\n([^\n]*)/gs
DEMO
OR
/<textarea id[^>]*>(.*?)(?=<\/textarea>)/gs
DEMO
Captured group1 conatins the string this is some text
OR
you could use the below regex to match only the string this is some text.
/div class="form-item.*class="form-textarea">[^\n]*\n\K[^\n]*/s
DEMO

Using php preg_replace_callback - but replacing whole string not each sub string [duplicate]

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

Categories