PHP Regular Expression: String starts with and ends with - php

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

Related

how to cut tag in string with PHP

I Have a problem with this output receive value.
$simple="<TRAN_ID>17564_36428.1354_4159</TRAN_ID>
<TRAN_DATE>20160201</TRAN_DATE>
<TRAN_TIME>10:07:08</TRAN_TIME>
<ERROR_CODE>1</ERROR_CODE>
<ERROR_DESC>Not Input Policy</ERROR_DESC>
<POLICY_NBR></POLICY_NBR>";
I want to cut the code with PHP.
TRAN_ID = ?
TRAND_DATE = ?
ERROR_CODE = ?
ERROR_DESC = ?
How can i do it. sorry my english is bad.
Thanks.
You can use PHP's SimpleXML library, like so:
<?php
$str ="<TRANS><TRAN_ID>17564_36428.1354_4159</TRAN_ID><TRAN_DATE>20160201</TRAN_DATE><TRAN_TIME>10:07:08</TRAN_TIME><ERROR_CODE>1</ERROR_CODE><ERROR_DESC>Not Input Policy</ERROR_DESC><POLICY_NBR></POLICY_NBR></TRANS>";
$transaction = simplexml_load_string($str);
echo $transaction->TRAN_ID.PHP_EOL;
echo $transaction->TRAN_DATE.PHP_EOL;
echo $transaction->TRAN_TIME.PHP_EOL;
echo $transaction->ERROR_CODE.PHP_EOL;
echo $transaction->ERROR_DESC.PHP_EOL;
echo $transaction->POLICY_NBR.PHP_EOL;
Note that I added <TRANS> start and end tags to your string.
If the data always looks like the sample, this should work okay.
<?php
function getTextBetweenTags($string, $tagname) {
$pattern = "/<$tagname ?.*>(.*)<\/$tagname>/";
preg_match($pattern, $string, $matches);
return $matches[1];
}
$str = '<textformat leading="2"><p align="left"><font size="10">get me</font></p></textformat>';
$txt = getTextBetweenTags($str, "font");
echo $txt;
?>

Replace string between two char(special symbol) in 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
);

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

Replace 2 \n in text to <br \> in php?

i know the nl2br function in php to replace '\n' to <br \>,
but how to replace \n\n to <br \>.
for example:
string=
'paragraph
1
paragraph2'
it is expected to show
paragraph1
paragraph2
p.s. i have a form to input the text.
Any help is most appreciated :)
Replace it easily with some simple regex:
preg_replace('/\n+/', '<br/>', $string);
try this
$string=
'paragraph
1
paragraph2';
$arr_temp = explode("\n", $string);
$prev_blank = false;
foreach($arr_temp as $str)
{
if(trim($str)=="")
{
if(!$prev_blank)
{
echo "<br/>";
$prev_blank = true;
}
}
else
{
echo trim($str);
$prev_blank = false;
}
}
output:
paragraph1
paragraph2
i've solved the problem.
just use
str_replace("\n\r\n", "<br />", $model->solutionText);

Categories