2010-09-22 3 views
0

J'ai ce code:PHP: String format remplacer et ajouter du caractère

$array[] = "<b> FLYDANNA&reg; HEADWRAP </b>  <ul> <li type=\"circle\"><i> Sensational headwrap made from 6 pieces of fabric to fit the contours of your head perfectly </i> <li type=\"circle\"><i> Strong yet lightweight cotton wrap can be folded up and stored almost anywhere-;pocket, purse, backpack ... you name it! </i> <li type=\"circle\">"; 

Je veux supprimer toutes les balises html, supprimer les espaces en excès et ajouter un double tiret après le dernier mot du capital. De plus, après chaque balise </li>, j'ajouterai une période.

J'ai besoin de le formater avant de le mettre dans le tableau de sorte que la sortie doit être:

$array[] = "FLYDANNA&reg; HEADWRAP-- Sensational headwrap made from 6 pieces of fabric to fit the contours of your head perfectly. Strong yet lightweight cotton wrap can be folded up and stored almost anywhere-;pocket, purse, backpack ... you name it!"; 

Merci à l'avance :)

Répondre

3

Dans le cas suivant:

$str = "<b> FLYDANNA&reg; HEADWRAP </b>  <ul> <li type=\"circle\"><i> Sensational headwrap made from 6 pieces of fabric to fit the contours of your head perfectly </i> <li type=\"circle\"><i> Strong yet lightweight cotton wrap can be folded up and stored almost anywhere-;pocket, purse, backpack ... you name it! </i> <li type=\"circle\">"; 

Cette code devrait faire l'affaire:

$str = strip_tags($str); 
$str = preg_replace('/\s+/', ' ', $str); 
$words = array_reverse(explode(' ', $str)); 
foreach ($words as $k => $s) { 
    if (preg_match('/\b[A-Z]{2,}\b/', $s)) { 
    $words[$k] = $s . "--"; 
    break; 
    } 
} 

$str = trim(join(' ', array_reverse($words))); 
+0

Merci monsieur pnomolos pour la réponse rapide! : D Cheers :) Je vais l'essayer maintenant – marknt15