KaLv1n K
KaLv1n K

Reputation: 131

PHP - preg multiple result

i have code like this:

<?
$source = '!Hello, this is !PHP!HTML !@language';
preg_match("'!(.*?)'si", $source, $match);
if($match) print_r($match);
?>

and i want to get result like:

!Hello
!PHP
!HTML
!@language

anyone can help?

Upvotes: 0

Views: 67

Answers (4)

WordsWorth
WordsWorth

Reputation: 872

Use this instead of preg_match

preg_match_all("/![@a-z]*/si", $source, $match);

preg_match returns only first match.

Upvotes: 0

Ayush
Ayush

Reputation: 42450

<?php
    $haystack = "!Hello, this is !PHP!HTML !@language";
    $needle = "/!([^\s\!,])*/";

    $matches;
    preg_match_all($needle,$haystack,$matches);

    foreach($matches[0] as $match)
    {
        echo $match . "\n";
    }   
?>

Upvotes: 0

xdazz
xdazz

Reputation: 160883

$source = '!Hello, this is !PHP!HTML !@language';
preg_match_all("~![^(!|\s|,)]*~si", $source, $match);
if($match) print_r($match);

Upvotes: 0

scessor
scessor

Reputation: 16115

Change to:

preg_match_all("/![a-z@]*/i", $source, $match);

Also see this example.

Upvotes: 4

Related Questions