I want the word between characters
'#' ,'_'
Change to
$word
Example :
#Ali_ is a good boy, I and #Jan_ love him
change to
Ali is a good boy, I and Jan love him
Thanks for your response
$string = 'ae #ali er #hassan sss';
$string = preg_replace("/#(.+?) /is", "<a href='Profile/$1'>$1</a>", $string);
echo($string);
Tried and tested:
function replaceString($content = '') {
preg_match_all('/#(.*?)_/', $content, $matches);
if(is_array($matches[0])) {
foreach($matches[0] as $key => $match) {
$new = $match;
$new = str_replace('#', '', $new);
$new = str_replace('_', '', $new);
$new = '' . $new . '';
$content = str_replace($match, $new, $content);
}
}
return $content;
}
The output of the following:
$content = '#name1_ and this other #name2_';
$content = replaceString($content);
echo $content;
Will be:
name1 and this other name2
Related
I have a codeigniter helper called xssclean for input validating form data
If i give array it show each deprecated error.
Here is my function in my xssclean_helper.php
function xssclean($str) {
if (is_array($str)) {
while (list($key) = each($str)) {
$str[$key] = $xssclean($str[$key]);
}
return $str;
}
$str = _remove_invisible_characters($str);
$str = preg_replace('|\&([a-z\_0-9]+)\=([a-z\_0-9]+)|i', _xss_hash() . "\\1=\\2", $str);
$str = preg_replace('#(&\#?[0-9a-z]{2,})([\x00-\x20])*;?#i', "\\1;\\2", $str);
$str = preg_replace('#(&\#x?)([0-9A-F]+);?#i', "\\1\\2;", $str);
$str = str_replace(_xss_hash(), '&', $str);
$str = rawurldecode($str);
$str = preg_replace_callback("/[a-z]+=([\'\"]).*?\\1/si", '_convert_attribute', $str);
$str = preg_replace_callback("/<\w+.*?(?=>|<|$)/si", '_html_entity_decode_callback', $str);
$str = _remove_invisible_characters($str);
$str = _remove_tabs($str);
$str = _never_allowed_str($str);
$str = _never_allowed_regx($str);
$str = str_replace(array('<?', '?' . '>'), array('<?', '?>'), $str);
$str = _never_allowed_words($str);
do {
$original = $str;
if (preg_match("/<a/i", $str)) {
$str = preg_replace_callback("#<a\s+([^>]*?)(>|$)#si", '_js_link_removal', $str);
}
if (preg_match("/<img/i", $str)) {
$str = preg_replace_callback("#<img\s+([^>]*?)(\s?/?>|$)#si", '_js_img_removal', $str);
}
if (preg_match("/script/i", $str) OR preg_match("/xss/i", $str)) {
$str = preg_replace("#<(/*)(script|xss)(.*?)\>#si", '', $str);
}
} while ($original != $str);
unset($original);
$event_handlers = array('[^a-z_\-]on\w*', 'xmlns');
$str = preg_replace("#<([^><]+?)(" . implode('|', $event_handlers) . ")(\s*=\s*[^><]*)([><]*)#i", "<\\1\\4", $str);
$naughty = 'alert|applet|audio|basefont|base|behavior|bgsound|blink|body|embed|expression|form|frameset|frame|head|html|ilayer|iframe|input|isindex|layer|link|meta|object|plaintext|style|script|textarea|title|video|xml|xss';
$str = preg_replace_callback('#<(/*\s*)(' . $naughty . ')([^><]*)([><]*)#is', '_sanitize_naughty_html', $str);
$str = preg_replace('#(alert|cmd|passthru|eval|exec|expression|system|fopen|fsockopen|file|file_get_contents|readfile|unlink)(\s*)\((.*?)\)#si', "\\1\\2(\\3)", $str);
$str = _never_allowed_str($str);
$str = _never_allowed_regx($str);
return $str;
}
At line no 3 i get error
The each() function is deprecated with PHP 7.2. But you can replace your while-loop with a foreach-loop:
function xssclean($str) {
if (is_array($str)) {
foreach($str as &$value){
$value = xssclean($value);
}
return $str;
}
// …
}
The $value variable is per default a copy of the array value. The & makes it a reference, this way you can update the value.
Manipulating the array while iterating over it, is not a good idea and can lead to errors.
I have a series of tag in the text that I want to match with preg_match_all but the $regex string has something wrong and I can't understand what to do.
$tagReplace = ['mov', 'flv', 'youtube'];
foreach ($tagReplace as $plg_tag) {
$regex = "#{" . $plg_tag . "}(.*?){/" . $plg_tag . "}#s";
$var = preg_match_all($regex, "{mov}the_video_url{/mov}", $matches, PREG_PATTERN_ORDER);
var_dump($matches);
}
<?php
$matches = array();
$p = '/(mov|flv|youtube)/';
$replace_to = 'your text you want to be insted of the mov or flv or youtube';
$str = ' the string that you want to work on and replace its content';
$p_c = array(
$p => function($match) {
$ret = str_replace("mov", $replace_to, $match[0]);
$ret = str_replace("flv", $replace_to, $ret);
$ret = str_replace("youtube", $replace_to, $ret);
return $ret;
}
);
$res3 = preg_replace_callback_array($p_c, $str);
I have this string...
$text = "1|2|1400|34|A|309|Frank|william|This|is|the|line|here|"
How do I replace all the occurrences of | with " " after the 8th occurrence of | from the beginning of the string?
I need it look like this, 1|2|1400|34|A|309|Frank|william|This is the line here
$find = "|";
$replace = " ";
I tried
$text = preg_replace(strrev("/$find/"),strrev($replace),strrev($text),8);
but its not working out so well. If you have an idea please help!
You can use:
$text = '1|2|1400|34|A|309|Frank|william|This|is|the|line|here|';
$repl = preg_replace('/^([^|]*\|){8}(*SKIP)(*F)|\|/', ' ', $text);
//=> 1|2|1400|34|A|309|Frank|william|This is the line here
RegEx Demo
Approach is to match and ignore first 8 occurrences of | using ^([^|]*\|){8}(*SKIP)(*F) and the replace each | by space.
You can use explode()
$text = "1|2|1400|34|A|309|Frank|william|This|is|the|line|here|";
$arr = explode('|', $text);
$result = '';
foreach($arr as $k=>$v){
if($k == 0) $result .= $v;
else $result .= ($k > 7) ? ' '.$v : '|'.$v;
}
echo $result;
You could use the below regex also and replace the matched | with a single space.
$text = '1|2|1400|34|A|309|Frank|william|This|is|the|line|here|';
$repl = preg_replace('~(?:^(?:[^|]*\|){8}|(?<!^)\G)[^|\n]*\K\|~', ' ', $text);
DEMO
<?php
$text = "1|2|1400|34|A|309|Frank|william|This|is|the|line|here|";
$texts = explode( "|", $text );
$new_text = '';
$total_words = count( $texts );
for ( $i = 0; $i < $total_words; $i++)
{
$new_text .= $texts[$i];
if ( $i <= 7 )
$new_text .= "|";
else
$new_text .= " ";
}
echo $new_text;
?>
The way to do that is:
$text = "1|2|1400|34|A|309|Frank|william|This|is|the|line|here|";
$arr = explode('|', $text, 9);
$arr[8] = strtr($arr[8], array('|'=>' '));
$result = implode('|', $arr);
echo $result;
Example without regex:
$text = "1|2|1400|34|A|309|Frank|william|This|is|the|line|here|";
$array = str_replace( '|', ' ', explode( '|', $text, 9 ) );
$text = implode( '|', $array );
str_replace:
If subject is an array, then the search and replace is performed with
every entry of subject, and the return value is an array as well.
explode:
If limit is set and positive, the returned array will contain a
maximum of limit elements with the last element containing the rest of
string.
I want to remove words from sentence if word contains #, I am using php.
Input: Hi I am #RaghavSoni
Output: Hi I am
Thank You.
You could do:
$str = preg_replace('/#\w+/', '', $str);
This is not a good way, but it works :
<?php
$input="Hi I am #RaghavSoni";
$inputWords = explode(' ', $input);
foreach($inputWords as $el)
{
if($el[0]=="#" )
{
$input = str_replace($el, "", $input);
}
}
echo $input;
?>
while(strpos($string, '#') !== false) {
$location1 = strpos($string, "#");
$location2 = strpos($string, " ", $location1);
if($location2 !== false) {
$length = $location2 - $location1;
$string1 = substr($string, 0, $location1);
$string2 = substr($string, $location2);
$string = $string1 . $string2;
}
}
echo $string;
echo str_replace("#RaghavSoni", "", "Hi I am #RaghavSoni.");
# Output: Hi I am.
I have a string like this one text more text "empty space".
How can I replace the space in "empty space" and only this space with ###?
$string = 'text more text "empty space"';
$search = 'empty space';
str_replace($search, 'empty###space', $string);
How about this, with no regular expressions:
$text = 'foo bar "baz quux"';
$parts = explode('"', $text);
$inQuote = false;
foreach ($parts as &$part) {
if ($inQuote) { $part = str_replace(' ', '###', $part); }
$inQuote = !$inQuote;
}
$parsed = implode('"', $parts);
echo $parsed;
$somevar = "empty space";
$pattern = "/\s/";
$replacement = "###";
$somevar2 = preg_replace($pattern, $replacement, $somevar);
echo $somevar2;
$string = "My String is great";
$replace = " ";
$replace_with = "###";
$new_string = str_replace($replace, $replace_with, $string);
This should do it for you. http://www.php.net/manual/en/function.str-replace.php
Edited after you comments
Maybe it's not the best solution, but you can do it like this:
$string = 'text more text "empty space"';
preg_match('/(.*)(".*?")$/', $string, $matches);
$finaltext = $matches[1] . str_replace(' ', '###', $matches[2]);