parsing using php
given a string :
The [[Pulsatile_flow|pulsatile]] nature of blood flow creates a pulse wave that is propagated down the [[arterial tree]], and at 开发者_如何学Python [[Aortic_bifurcation|bifurcations]] reflected waves rebound to return to semilunar valves and the origin of the [[aorta]]. These return waves create the [[Dicrotic_notch#Ventricular_systole|dicrotic notch]] displayed in the aortic pressure curve during the [[cardiac cycle]] as these reflected waves push on the [[heart valve|aortic semilunar valve]].[[Wiktionary:aorta|]]
how could i extract ALL words/phrases enclosed in '[[ ]]' and put then in an array using php.
with condition : if "|" exist retrieve only words after "|" if no words exist after "|" retrieve the words before "|" but after ":" .
words in parenthesis will also be disregarded.example
[[aorta]] => retrieve aortal
[[Pulsatile_flow|pulsatile]] => retrieve only pulsatile
[[Pulsatile_flow|pulsatile (temp)]] => retrieve only pulsatile
[[Wiktionary:aorta|Aorta Topic]] => retrieve Aorta Topic
[[Wiktionary:aorta|]] => retrieve aorta
[[aorta|]] => retrieve aorta
if "|" did not exists retrieve all
[[Wiktionary:aorta]] => retrieve Wiktionary:aorta
Check this out:
$results = array();
$source = "The [[Pulsatile_flow|pulsatile]] nature of blood flow creates a pulse wave that is propagated down the [[arterial tree]], and at [[Aortic_bifurcation|bifurcations]] reflected waves rebound to return to semilunar valves and the origin of the [[aorta]]. These return waves create the [[Dicrotic_notch#Ventricular_systole|dicrotic notch]] displayed in the aortic pressure curve during the [[cardiac cycle]] as these reflected waves push on the [[heart valve|aortic semilunar valve]].[[Wiktionary:aorta|]]";
if ( preg_match_all('/\[\[(.*?)\]\]/is', $source, $matches) )
{
foreach ( $matches[1] as $match )
{
if ( strpos( $match, '|' ) === false ) {
$results[] = $match;
} else {
$parts = explode( '|', $match );
if ( empty( $parts[1] ) ) {
$parts = explode( ':', $parts[0] );
$results[] = count( $parts ) == 2 ? $parts[1] : $parts[0];
} else {
$results[] = preg_replace( '/\(.*?\)/is', '', $parts[1] );
}
}
}
}
var_dump( $results );
精彩评论