Convert emoji into custom images [duplicate] - php

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

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>

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;

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

Search percent matching text in html text [duplicate]

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;

finding URL in a text plain and change it to hyperlink [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Find url from string with php
I found this code:
but 2 link did not make properly.
save my code in a php file for testing.
$a = <<<_END
www.coders.com/coder6602.html => Work
www.avan.ir?a=21 => Work
www.limited.ndot.in/kansas/#?w=500 => Not Work
http://www.sales.com => Work
http://www.mediafire.com/?298nlpla3eys9g6 => Work
http://diary.com/files/draft.html Ùˆ http://www.logo.net/plogo.swf => Not Work (this is two link)
_END;
/*
limited.ndot.in/kansas/#?w=500
diary.com/files/draft.html Ùˆ logo.net/plogo.swf
*/
function strHyperlink($str){
$pattern_url = '~(?>[a-z+]{2,}://|www\.)(?:[a-z0-9]+(?:\.[a-z0-9]+)?#)?(?:(?:[a-z](?:[a-z0-9]|(?<!-)-)*[a-z0-9])(?:\.[a-z](?:[a-z0-9]|(?<!-)-)*[a-z0-9])+|(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))(?:/[^\\/:?*"<>|\n]*[a-z0-9])*/?(?:\?[a-z0-9_.%]+(?:=[a-z0-9_.%:/+-]*)?(?:&[a-z0-9_.%]+(?:=[a-z0-9_.%:/+-]*)?)*)?(?:#[a-z0-9_%.]+)?~i';
$str = preg_replace('/http:\/\//','www.', $str);
$str = preg_replace('/www.www./','www.', $str);
$str = preg_replace($pattern_url,"\\0", $str);
return preg_replace('/www./','',$str);
}
echo strHyperlink($a);
Use this regex: \b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))
You can test it on: http://gskinner.com/RegExr/
I have just tested this regex with your url's and works perfectly.

Categories