Run the function in all parts of the php line

I built a function that will capture text between brackets and output them as an array. But the problem is that my function is only executed for the first time in a string.

function GetBetween($content,$start,$end){ $r = explode($start, $content); if (isset($r[1])){ $r = explode($end, $r[1]); return $r[0]; } return ''; } function srthhcdf($string){ $innerCode = GetBetween($string, '[coupon]', '[/coupon]'); $iat = explode('&&', $innerCode); $string = str_replace('[coupon]','',$string); $string = str_replace('[/coupon]','',$string); $newtext = '<b>'.$iat[0].'</b> <i>'.$iat[1].'</i><b>'.$iat[2].'</b>'; $string = str_replace($innerCode,$newtext,$string); return $string; } $Text = srthhcdf($Text); 

But it matches only the first [coupon] and [/ coupon] not the others. For example, when the line

 hello world [coupon]hello && bad && world[/coupon] and also to [coupon] the && bad && world [/coupon] 

He outputs

 Hello world <b>hello </b> <i> bad </i><b> world</b> and also to the && bad && world. 

This means that every time it replaces [coupon] and [/coupon] , but does not format the text inside them every time.

+5
source share
4 answers

Check out my solution. Your problem is that you replace the codes after the first call, and there is no loop:

 function GetBetween($content, $start, $end) { $pieces = explode($start, $content); $inners = array(); foreach ($pieces as $piece) { if (strpos($piece, $end) !== false) { $r = explode($end, $piece); $inners[] = $r[0]; } } return $inners; } function srthhcdf($string) { $innerCodes = GetBetween($string, '[coupon]', '[/coupon]'); $string = str_replace(array('[coupon]', '[/coupon]'), '', $string); foreach ($innerCodes as $innerCode) { $iat = explode('&&', $innerCode); $newtext = '<b>' . $iat[0] . '</b> <i>' . $iat[1] . '</i><b>' . $iat[2] . '</b>'; $string = str_replace($innerCode, $newtext, $string); } return $string; } $testString = "hello world [coupon]hello && bad && world[/coupon] and also to [coupon] the && bad && world [/coupon]"; $Text = srthhcdf($testString); echo $Text; 
+1
source

using regex will be an easy solution for this kind of thing

 $Text = "hello world [coupon]hello && bad && world[/coupon] and also to [coupon] the && bad && world [/coupon]"; $result = preg_replace('%\[coupon]([^[]*)\[/coupon]%', '<i>\1</i>', $Text); print $result; 
+1
source

Try using this code:

 $Text = 'hello world [coupon]hello && bad && world[/coupon] and also to [coupon] the && bad && world [/coupon]'; echo preg_replace('#\[coupon\][\s]*(\w+)([\s&]+)(\w+)([\s&]+)(\w+)[\s]*\[\/coupon\]#i', '<b>$1</b> <i>$3</i><b>$5</b>', $Text); 
+1
source

Solution with ReGex (captures all text and [coupon] [/ coupon] and replaces them with a new line

 preg_replace('#\[coupon\][A-Z0-9]+\[/coupon\]#i', $replaceText, $content); 

if you want to save the [coupon] tags:

 preg_replace('#(\[coupon\])[A-Z0-9]+(\[/coupon\])#i', '$1'.$replaceText.'$2', $content); 
0
source

Source: https://habr.com/ru/post/1209219/


All Articles