Based on a question I had answered here ( Use PHP to Replace HTML with HTML ), I'd like to be able to filter my output text for email addresses, and convert those text emails to "mailto" links.
Here's the PHP code that works, but only for converting some HTML to other HTML. What I've tried to do is have this function look for an email address, and convert it to a "mailto" link. For whatever reason, the code doesn't convert the email addresses. Here's my PHP:
function text_filter($string) {
$search = array('<p>__</p>', '/[a-zA-Z0-9._-]+#[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}/');
$replace = array('<hr />', '$2');
$processed_string = str_replace($search, $replace, $string);
echo $processed_string;
}
When I use this function for output, this is what the code looks like:
<?php text_filter( get_the_content() ); ?>
str_replace() doesn't use regular expressions, rewritten with preg_replace().
Added delimiters to first matching expression.
Fixed replacement from $1 to $2.
function text_filter($string) {
$search = array('/<p>__<\/p>/', '/([a-zA-Z0-9._-]+#[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4})/');
$replace = array('<hr />', '$1');
$processed_string = preg_replace($search, $replace, $string);
echo $processed_string;
}
#Adam Baney - This will work even when used repeatedly.
// EMAILS
$str = preg_replace('~(^|[\s\.,;\n\(])([a-zA-Z0-9._+-]+#[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4})~',
'$1$2',
$str);
// PHONE NUMBERS
$str = preg_replace_callback('~(^|[\s\.,;\n\(])(?<! )([0-9 \+\(\)]{9,})~', function($m) {
return $m[1].''.$m[2].'';
}, $str);
You can't use str_replace to do a regular expression replace.
You will need to split the actions up.
function text_filter($string) {
$search = array('<p>__</p>');
$replace = array('<hr />');
$processed_string = str_replace($search, $replace, $string);
$processed_string = preg_replace('/[a-zA-Z0-9._-]+#[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}/','$2',$processed_string);
echo $processed_string;
}
See: http://www.php.net/manual/en/function.preg-replace.php for preg replacing.
function obfuscate_email($content){
$pattern = '#([0-9a-z]([-_.]?[0-9a-z])*#[0-9a-z]([-.]?[0-9a-z])*\\.';
$pattern .= '[a-wyz][a-z](fo|g|l|m|mes|o|op|pa|ro|seum|t|u|v|z)?)#i';
$replacement = '\\1';
$content = preg_replace($pattern, $replacement, $content);
return $content;
}
And add filter
add_filter( 'the_content', 'obfuscate_email' );
Another way to do it in order so that it would work with existing html links in text:
function html_parse_text($text)
{
$text = preg_replace("/(?<!\")(((f|ht){1}tps?:\/\/)[-a-zA-Z0-9#:%_\+.~#?&\/\/=]+)/",
'<a href="\\1" target=_blank>\\1</a>', $text);
$text = preg_replace("/([[:space:]()[{}])(www.[-a-zA-Z0-9#:%_\+.~#?&\/\/=]+)/",
'\\1<a href="http://\\2" target=_blank>\\2</a>', $text);
$text = preg_replace("/(?<!\")([_\.0-9a-z-]+#([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})/",
'<a href="mailto:\\1" target=_blank>\\1</a>', $text);
return $text;
}
Here is another version of this that seems to work for me. I've added + char to handle "plus addressing" (like some+email#address.com)
function replaceemail($text) {-
$ex = "/([a-zA-Z0-9._+-]+#[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4})/";
preg_match_all($ex, $text, $url);
foreach($url[0] as $k=>$v) $text = str_replace($url[0][$k], ''.$url[0][$k].'', $text);
return $text;
}
Related
$cont=htmlspecialchars(file_get_contents("https://myanimelist.net/anime/30276/One_Punch_Man"));
function getBetween($string, $start = "", $end = ""){
if (strpos($string, $start)) { // required if $start not exist in $string
$startCharCount = strpos($string, $start) + strlen($start);
$firstSubStr = substr($string, $startCharCount, strlen($string));
$endCharCount = strpos($firstSubStr, $end);
if ($endCharCount == 0) {
$endCharCount = strlen($firstSubStr);
}
return substr($firstSubStr, 0, $endCharCount);
} else {
return '';
}
}
$name=getBetween($cont,'title',' - MyAnimeList.net');
//$name=preg_replace('/[^a-zA-Z0-9 \p{L}]/m', '', $name);
preg_replace('/(*UTF8)[\>\<]/m', '', $name);
trim($name," ");
//$name=str_replace("gt", "", $name);
echo $name;
i want to find the text between title tags. how to do this?
for example in this page title contains 'One Punch Man - MyAnimeList.net' i want to get that
Just use string replace function:
$string = '<BoomBox>';
$string = str_replace('<', '', $string);
$string = str_replace('>', '', $string);
echo $string; // output: Boombox
http://php.net/manual/en/function.str-replace.php
You edited your answer, and we can now see you are dealing with XML/HTML. It's always better to work with the DOM classes. Never use regex! There is a famous Stack Overflow post explaining why never to parse html with regex. Try this solution instead:
<?php
$dom = new DOMDocument();
$dom->loadHTML('<title>BoomBox</title>');
echo $dom->getElementsByTagName('title')->item(0)->textContent;
http://php.net/manual/en/class.domdocument.php
http://php.net/manual/en/class.domnode.php
See it working here https://3v4l.org/EjPQd
You can use preg_replace();, or strip_tags();.
Example preg_replace();:
$str = '> One Punch Man';
$new = preg_replace('/[^a-zA-Z0-9 \p{L}]/m', '', $str);
echo $new;
Output: One Punch Man
Above example will only allow a-z, A-Z and 0-9. You can expand this.
Example strip_tags();:
$str = '<title> BoomBox </title>';
$another = strip_tags($str);
echo $another;
Output: BoomBox
Documentation:
http://php.net/manual/en/function.preg-replace.php // preg_replace();
http://php.net/manual/en/function.strip-tags.php // strip_tags();
You can also use a single call to str_replace with the ['<','>'] as the search argument:
$string = '<BoomBox>';
echo str_replace(['<', '>'], '', $string) . PHP_EOL;
// => Boombox
Or, you may use a regex with preg_replace (especially, if you plan on adding more restrictions for in-context matching to it):
echo preg_replace('~[<>]~', '', $string);
// => Boombox
See the PHP demo.
I need to extract all links in a body of text in php and make them clickable. The problem is I can't seem to simplify the text of the link in any way.
I tried using preg_replace_callback but I can't seem to get the trimming function working properly:
function trimUrl($url){
$maxLength = 3;
if(strlen($url)>$maxLength){
$urlShort = substr($str,0,$maxLength).'...';
}
else{
$urlShort = $url;
}
return $urlShort;
}
function enableLinks($text){
return preg_replace_callback("!(((f|ht)tp(s)?://)[-a-zA-Zа-яА-Я()0-9#:%_+.~#?&;//=]+)!i", "<a href='$1' target='_blank'>".trimUrl("$1")."</a>", $text);
}
enableLinks("Visit more work at http://www.google.com");
How can I run a second function within the preg_replace_callback that trims the output text?
What if you used a function inside that function. So if the first function evaluates to true then run this next function? And also try using preg_replace_callback in a variable format so its easier to work with
First, you are using substring(). Where have you defined the variable $str? And, if you do this:
$var = preg_replace_callback("!(((f|ht)tp(s)?://)[-a-zA-Zа-яА-Я()0-9#:%_+.~#?&;//=]+)!i", "<a href='$1' target='_blank'>".trimUrl("$1")."</a>", $text);
Than can you use a new function:
return function($var);
Ended up using a more expanded function to achieve this, works on multiple urls with or without "http://":
function trimUrlOutput($url){
$maxLength = 30;
if(strlen($url)>$maxLength){
$urlShort = substr($url,0,$maxLength).'...';
}
else{
$urlShort = $url;
}
return $urlShort;
}
function enableLinks($text){
$text = ereg_replace( "www\.", "http://www.", $text );
$text = ereg_replace( "http://http://www\.", "http://www.", $text );
$text = ereg_replace( "https://http://www\.", "https://www.", $text );
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
if(preg_match_all($reg_exUrl, $text, $url)) {
$matches = array_unique($url[0]);
foreach($matches as $match) {
$linkText = trimUrlOutput($match);
$replacement = "<a href=".$match." target='_blank'>{$linkText}</a>";
$text = str_replace($match,$replacement,$text);
}
return $text;
}
else{
return $text;
}
}
enableLinks("Visit more work at http://www.google.com");
Hope this helps someone.
How to delete text after !--more-- tag?
I tried:
function addMore($text) {
$text = preg_replace('/!--more--(.*)/', ' ', $text);
return $text;
}
Thanks in advance.
Just delete the string, replacing it with the !--more--:
$text = preg_replace('/!--more--.*/', '!--more--', $text);
$text = "plain to you howhings of the great explorer of the truth.!--more-- plain to you how allt the truth,/";
echo $short = substr($text, 0, strpos( $text, '!--more--'));
Output:
http://codepad.org/atXzthBc
Add the 's' modifier to make the preg work even on break lines:
function addMore($text) {
$text = preg_replace('/!--more--(.*)/s', ' ', $text);
return $text;
}
look here:
http://php.net/manual/en/reference.pcre.pattern.modifiers.php
YOu can use explode function or simply substr as below :
$text = substr($text, 0, strpos($text, '!-more-'));
function addMore($text) {
$newtext=explode("!--more--",$text);
return $newtext[0];
}
function stripSingleEndedTag($content, $allowed = array()){
(array)$allowed;
$singletags = array('<meta>','<img>','<input>','<hr>','<br>','<link>','<isindex>','<base>','<meta>','<nextid>','<bork>');
$stripthese = arrayfilterout ($singletags, $allowed);
$stripthese = str_replace(array('<','>'),'',$stripthese);
$pattern = '/<('. implode('|', $stripthese) .')[^>]+\>/i';
$content = preg_replace($pattern, "", $content);
return $content;
}
What I've got here will strip out a single ended tag like <bork /> but only if there is some character after 'bork' and before '>'. <bork > and <bork/> are stripped, but not <bork>
BTW, cant use strip_tags().
You can use:
$pattern = '/\<('. implode('|', $stripthese) .')[^>]*\>/i';
Original Answer:
Do you want to get rid of <bork /> and <bork/>, but not <bork> and <bork >?
It looks like what you want is:
$pattern = '/<('. implode('|', $stripthese) .').*\\\>/i';
Update: fix for greedy match:
$pattern = '/<('. implode('|', $stripthese) .').*?\\\>/i';
$pattern = '/<('. implode('|', $stripthese) .')[^>]*\\\>/i';
I have this bbcode tag "remover" which should remove bbcode tags from my test text.
All i get is nothing. Just blank page where should be the text replaced with html tags.
Whats wrong with it. And maybe anyone have some better script to share.
$str = 'This [b]is just[/b] a [i]test[/i] text!';
function forum_text($str)
{
$str = htmlspecialchars($str);
$str = preg_replace( "#\[url\](?:http:\/\/)?(.+?)\[/url\]#is", "$1", $str );
$str = preg_replace( "#\[img\](?:http:\/\/)?(.+?)\[/img\]#is", "<img src=\"http://$1\" />", $str );
$str = preg_replace( "#\[b\](.+?)\[/b\]#is", "<strong>$1</strong>", $str );
$str = preg_replace( "#\[i\](.+?)\[/i\]#is", "<i>$1</i>", $str );
$str = preg_replace( "#\[u\](.+?)\[/u\]#is", "<u>$1</u>", $str );
return $str;
}
The following is your code, with some code in front of it (to make sure any errors are shown) and some code at the back (that actually calls your function).
If this doesn't work for you, your problem is not here, unless you don't have a working PCRE.
error_reporting(-1); ini_set('display_errors', 'On');
$str = 'This [b]is just[/b] a [i]test[/i] text!';
function forum_text($str)
{
$str = htmlspecialchars($str);
$str = preg_replace( "#\[url\](?:http:\/\/)?(.+?)\[/url\]#is", "$1", $str );
$str = preg_replace( "#\[img\](?:http:\/\/)?(.+?)\[/img\]#is", "<img src=\"http://$1\" />", $str );
$str = preg_replace( "#\[b\](.+?)\[/b\]#is", "<strong>$1</strong>", $str );
$str = preg_replace( "#\[i\](.+?)\[/i\]#is", "<i>$1</i>", $str );
$str = preg_replace( "#\[u\](.+?)\[/u\]#is", "<u>$1</u>", $str );
return $str;
}
echo forum_text($str);