-1
votes

Regex commencer par un mot à un mot spécifique / char / espace

La chaîne pourrait être comme suit la ligne par ligne à chaque fois.

town=87
town=878
town="878"
town=8,43
town='8,43'
town=-1
town=N/A


3 commentaires

Pourquoi avez-vous besoin d'une regex pour cela? Strows et Substr sont assez suffisants pour cette tâche.


Oui je sais. Mais je veux y parvenir avec regex, veuillez répondre à la meilleure solution.


Je ne discuterai pas avec vous quelle méthode est la meilleure - mais si vous avez du mal à écrire une regex, vous pouvez envisager d'utiliser une méthode que vous comprenez parfaitement.


3 Réponses :


2
votes

Essayez d'utiliser Preg_Match_All CODE>, avec le motif de regex suivant:

$input = "code=876 and town=87 and geocode in(1,2,3)";
$input .= "code=876 and town=878 and geocode in(1,2,3)";
$input .= "code=876 and town=\"878\" and geocode in(1,2,3)";
$input .= "code=876 and town=8,43 and geocode in(1,2,3)";
$input .= "code=876 and town='8,43' and geocode in(1,2,3)";
$input .= "code=876 and town=-1 and geocode in(1,2,3)";
$input .= "code=876 and town=N/A and geocode in(1,2,3)";
preg_match_all("/town=\S+/", $input, $matches);
print_r($matches[0]);

Array
(
    [0] => town=87
    [1] => town=878
    [2] => town="878"
    [3] => town=8,43
    [4] => town='8,43'
    [5] => town=-1
    [6] => town=N/A
)


0 commentaires

2
votes

Utilisez exploser et exploser sur l'espace. XXX PRE>

Sorties: P>

town=87
town=878
town="878"
town=8,43
town='8,43'
town=-1
town=N/A


0 commentaires

1
votes

Utilisez exploser () fonction.

xxx


0 commentaires