2013-05-30 4 views
0

я получаю ответ от источника в строке какPhp извлекая текст между символами

page <a href="${CLICK_URL}" target="_top">site not active</a> 

Мне нужно извлечь эти данные в массив как:

Array 
(
    [0] => page 
    [1] => ${CLICK_URL} 
    [2] => _top 
    [3] => >site not active 
) 

У меня есть ключ, чтобы использовать preg_split(). Пожалуйста, помогите ...

+2

что исходная строка – kevinamadeus

+1

@kevinamadeus Вопрос отредактирован – Srikanth

+0

@ User12345 Как вы определяете 'page'? Regex - все о регулярных шаблонах. Я очень подозреваю, что 'page' - единственное слово в начале строки. – HamZa

ответ

3

Это работает для вас?

<?php 

$str = 'page <a href="${CLICK_URL}" target="_top">site not active</a>'; 

$regex = ' 
& 
    ^     # start of string 
    (.*)    # string before link 
    \s*     # whitespace 
    <a     # start of <a> tag 
    \s*     # whitespace 
    href="([^"]*)"  # match contents of href attribute 
    \s*     # whitespace 
    target="([^"]*)" # match contents of target attribute 
    >     # closing bracket of opening <a> tag 
    ([^<]*)    # match html between opening and closing <a> tag 
    </a>    # closing </a> tag 
    $     # end of string 
&x'; 

// the 'x' modifier enables the possibilty to add comments in a regex 
// http://php.net/manual/en/reference.pcre.pattern.modifiers.php 

preg_match(
    $regex, 
    $str, 
    $matches 
); 

print_r($matches); 

выход:

Array 
(
    [0] => page <a href="${CLICK_URL}" target="_top">site not active</a> 
    [1] => page 
    [2] => ${CLICK_URL} 
    [3] => _top 
    [4] => site not active 
) 
Смежные вопросы