Question

I get a response from a source in the string as

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

I need to extract this data to an array as:

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

I have clue to use preg_split() function. Please help...

Was it helpful?

Solution

Does this work for you?

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

output:

Array
(
    [0] => page <a href="${CLICK_URL}" target="_top">site not active</a>
    [1] => page
    [2] => ${CLICK_URL}
    [3] => _top
    [4] => site not active
)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top