2014-12-16 8 views
5

Zbudowałem funkcję, która przechwyci tekst między nawiasami i wypisze je jako tablicę. Ale problem polega na tym, że moja funkcja wykonuje się tylko pierwszy raz w ciągu znaków.Uruchomić funkcję na wszystkich częściach string php

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

Ale pasuje tylko pierwszy [kupon] i [/ kupon], a nie inne. Jak wtedy, gdy ciąg jest

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

wyprowadza ona

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

co oznacza, że ​​zastępuje [coupon] i [/coupon] za każdym razem, ale nie formatuje tekst w nich za każdym razem.

+0

może pętla pomoże –

+0

można zapewnić pl przykład ciąg i czego oczekujesz? – krzysiej

+0

, ale tekst jest pobierany z mysql i jest podobny do posta z wordpress. Ile razy powinienem uruchomić pętlę. – IdidntKnewIt

Odpowiedz

1

Sprawdź moje rozwiązanie. Twój problem jest, wymieniasz kody po pierwszej rozmowy, a nie ma pętli:

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; 
+0

Przykro mi to mówić, ale; Ostrzeżenie: podano niepoprawny argument dla foreach() w /srv/users/st/apps/cour/public/single.php on line 20 – IdidntKnewIt

+0

Dla mnie zadziałało. Wypróbuj pusty plik php. – vaso123

+0

Działa świetnie. Najlepsza odpowiedź na podstawie tego, czym jest pytanie. – IdidntKnewIt

0

Rozwiązanie z regex (przechwyci cały tekst i [kuponu] [/ kupon] i zastąpić je nowym String

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

jeśli chcesz oszczędzać [kupon] Tagi:

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

przy użyciu regex będzie proste rozwiązanie dla tego rodzaju rzeczy

$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

Spróbuj tego kodu:

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