Replace string between two char(special symbol) in php - php

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

Related

Replace content between two tags

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 = "/ *\(.*?\)/";

php replace paragraph to newline

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.

How to preg_replace pattern on string

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

PHP Regular Expression: String starts with and ends with

$ptn = "/^Response.+?[:] /";
$str = "Response from Moore Auto: Thanks for your feedback";
$rpltxt = "";
echo preg_replace($ptn, $rpltxt, $str);
"Moore Auto" is a variable name, so I simply need the text after the colon and space. Desired final result would be the string "Thanks for your feedback" in this case. Much appreciated!
Simple with substr() , like this:
$str = 'Response from Moore Auto: Thanks for your feedback';
echo substr($str, strpos($str,':')+2); //echoes "Thanks for your feedback"
Damiens solution does not work, if there is more than one colon. This should always work if the first part doesn't contain a colon:
<?php
$ptn = "/^Response[^:]+:\s*(.*)$/";
$str = "Response from Moore Auto: Thanks for your feedback";
if (preg_match($ptn, $str, $match)) {
$text = $match[1];
echo $text; //Thanks for your feedback
}
?>
try
<?php
$ptn = "/^(Response.+[:])(.*?)/";
$str = "Response from Moore Auto: Thanks for your feedback";
$rpltxt = "$2";
echo preg_replace($ptn, $rpltxt, $str);
?>

php string convert to html

I have a string <div id="myid">...</div>
How would I change this into
<div id="myid">...</div>
I have tried a few things but no luck any help?
Update
function get_page(){
$file = 'file.php';
$str = file_get_contents($file);
$str = html_entity_decode($str);
return $str;
}
$output = get_page();
echo $output;//don't work
FIX
function get_page(){
$file = 'file.php';
$str = file_get_contents($file);
return $str;
}
$output = get_page();
echo html_entity_decode($output);//works
The function for that is htmlspecialchars_decode().
Note that for the function to decode quotes, you need to specify the $quote_style parameter.
html_entity_decode is what you need: http://www.php.net/manual/en/function.html-entity-decode.php
$from = array('<', '>');
$to = array('<', '>');
$string = str_replace($from, $to, $string);
use this it's better ...
<?php
$text = '<p>Test paragraph.</p><!-- Comment --> Other text';
echo strip_tags($text);
echo "\n";
// Allow <p> and <a>
echo strip_tags($text, '<p><a>');
?>
htmlspecialchars_decode($string)

Categories