This is what I did till now:
<?php
$patterns= '/staff_(?)/';
$replacements= '';
$string = 'staff_name as user_name';
$string2 = 'staff_phone as user_phone';
echo preg_replace($patterns, $replacements, $string)."<br>";
echo preg_replace($patterns, $replacements, $string2);
?>
Output expecting is :
"staff_name as user_name" should return "name"
"staff_phone as user_phone" should return "phone"
The Regex way....
<?php
$str='staff_name as user_name';
echo $str = preg_replace("~staff_(.*?)_~","", $str); //"prints" name
$str='staff_name as user_phone';
echo $str = preg_replace("~staff_(.*?)_~","", $str); //"prints" phone
Demo
Non- Regex way using PHP native functions..
<?php
$str='staff_name as user_name';
$name_arr = explode('_',$str);
echo $name = array_pop($name_arr); //"prints" name
$str='staff_name as user_phone';
$phone_arr = explode('_',$str);
echo $phone = array_pop($phone_arr); //"prints" phone
Demo
Use this .*_(\w+$)
Do like this
<?php
$string = 'staff_name as user_name';
$string2 = 'staff_phone as user_phone';
echo preg_replace('/.*_(\w+$)/', '$1', $string)."\n";
echo preg_replace('/.*_(\w+$)/', '$1', $string2);
Demo
Related
How to remove 4th letter in string using PHP ?
I use this code.
<?php
$str = "1234567890";
$str2 = mb_substr($str, 4);
echo $str2;
?>
But it's will echo 567890
I want to echo 123567890 remove 4 from string.
How can i do ?
You can try substr_replace for this. Here we are replacing 4 which is at 3rd index.
Try this code snippet here
<?php
$str = "1234567890";
echo substr_replace($str, "", 3,1);
try setting the 3rd index to null
<?php
$str = "1234567890";
$str[3] = null;
echo $str;
try with below sulution:
$str = '1234567890';
$str_arr = str_split($str);
unset($str_arr[3]);
echo implode('', $str_arr);
output:
123567890
There are multiple ways of performing any operations on string variables in php
// can be used for printing purpose
$str = "1234567890";
echo substr($str,0,3).substr($str,4);
// actual replacement of string
$str = "1234567890";
echo substr_replace($str, "", 3,1);
I have a string like: He *is* a good boy. How *are* you. Then I want to replace is and are with input type textbox means replace things between asterisk(*). How can I get this Please help me out.
<?php
$buffer = 'He *is* a good boy. How *are* you.';
echo "Before: $buffer<br />";
$buffer = preg_replace_callback('/(\*(.*?)\*)/s', 'compute_replacement', $buffer);
echo "After: $buffer<br />";
function compute_replacement($groups) {
// $groups[1]: *item*
// $groups[2]: item
return '<input type="text" value="'.$groups[2].'" />';
}
?>
The result:
try this,
<?php
$x="hai, *was/is* are you, is this *was* test ";
echo preg_replace("/\*[\w\/]*\*/","",$x);
?>
Use preg_replace(); e.g:
<?php
$pattern = '/\*\w+\*/';
$string = 'he *is* a good boy';
$replacement = 'was';
echo preg_replace($pattern, $replacement, $string);
Yields:
he was a good boy
Try in this way:
$txt = "He *is* a good boy. How *are* you.";
$_GET['one'] = "doesn't";
$_GET['two'] = "think about";
preg_match_all( '{\*[^*]+\*}',$txt,$matches );
$txt = str_replace( $matches[0][0], $_GET['one'], $txt );
$txt = str_replace( $matches[0][1], $_GET['two'], $txt );
echo $txt;
3v4l.org demo
or, with preg_replace, in this way:
$txt = preg_replace
(
'/^(.*)\*[^*]+\*(.*)\*[^*]+\*(.*)$/', # <-- (Edited)
"\\1{$_GET[one]}\\2{$_GET[two]}\\3",
$txt
);
I made the following PHP function:
<?php
function convertGET($str) {
$regex = '/GET:+([a-zA-Z0-9_]+)/';
$str = preg_replace($regex, $_GET["$1"], $str);
return($str);
}
$string = "foobar: GET:foobar";
$string = convertGET($string);
echo $string;
?>
The function is suppost to get a string and replace something like:
GET:foobarwith the $_GET variable "foobar".
Use preg_replace_callback() instead:
<?php
$input = array("foobar" => "Some other string");
$regex = '~GET:([a-zA-Z0-9_]+)~';
$string = "foobar: GET:foobar";
$string = preg_replace_callback($regex,
function($matches) use ($input) {
return $input[$matches[1]];
},
$string);
echo $string;
// output: foobar: Some other string
?>
See a demo on ideone.com.
I want to replace all content between ( and ), using php.
my string:
$string = "This is a (string)";
the required output is:
$string = "This is a";
my code doesn't works:
$string = "This is a (string)";
$search = "/[^(](.*)[^)]/";
$string = preg_replace($search, "", $string);
echo $string; // output is ")"
$result = preg_replace('/\(.+?\)/sm', 'Put your text here', $string);
Try this code
Or to save () add them to replacement
$page = preg_replace('/\(.+?\)/sm', '(Put your text here)', $page);
This should work for you:
<?php
$string = "This is a (string)";
echo preg_replace("/\([^)]+\)/","",$string);
?>
Output:
This is a
Just change that:
$search = "/ *\(.*?\)/";
how to replace <p>hello</p> <p>world</p> to hello<br />world <br />
I've tried searching on stack but there is no matched result.
You could do this by using str_replace() function.
For instance:
$string = "<p>hello</p> <p>world</p>";
$string = str_replace('<p>', '', $string);
$string = str_replace('</p>', '<br />' , $string);
I try this myself and get what I expected
$pattern = '/<p>(\w+)\<\/p>/';
$subject = '<p>hello</p><p>world</p>';
$replacement = '${1}<br/>';
$out = preg_replace($pattern, $replacement, $subject);
I just wonder which is better regex or str_replace
I wrote a better solution, hope everybody can see it helpful and maybe improve it
$pattern = '/<p(.*?)>((.*?)+)\<\/p>/';
$replacement = '${2}<br/>';
$subject = 'html string';
$out = preg_replace($pattern, $replacement, $subject);
Use this to prevent breaking the first and last <p></p> :
$string = str_replace($string, '</p><p>', '');
But if a space comes between the tags, it won't work.
<?php
$str = '<p>hello</p> <p>world</p>';
$replaceArr = array('<p>', '</p>', '</p> <p>');
$replacementArr = array('', '', '<br />');
$str = str_replace($replaceArr, $replacementArr, $str);
echo $str;
?>
Try above code.