从变量php获取特定数据

时间:2012-07-13 03:49:05

标签: php

$vari="Love is an emotion of a strong <"/affection and personal/"> attachment. Love is also a virtue representing <"/all of human kindness/">, compassion, and affection";

查找从&lt;“/开头并以/”&gt;结尾的输出。

输出:

affection and personal
all of human kindness

感谢您的帮助

2 个答案:

答案 0 :(得分:1)

$vari='Love is an emotion of a strong <"/affection and personal/"> attachment. '
   . 'Love is also a virtue representing <"/all of human kindness/">, '
   . ' compassion, and affection';
preg_match_all('@<"/(.*?)/">@', $vari, $matches);
var_dump($matches[1]);

答案 1 :(得分:0)

假设整个事物都是正确转义的字符串,您可以使用此函数获取所有部分的数组

<?php

function cutMultiple($start, $end, $from)
{
    $results = array(); // init the output
    $step1 = explode($start, $from);    // get the results

    for( $i = 1; $i<count($step1); $i++)
    {
        $outputs = explode($end, $step1[$i]);   // get final cut
        $results[] = $outputs[0];   // append the cut
    }

    return $results;
}

$text = 'Love is an emotion of a strong <"/affection and personal/"> attachment. Love is also a virtue representing <"/all of human kindness/">, compassion, and affection';

echo cutMultiple('<"/', '/">', $text);

?>

或者采用正则表达式方法。

我必须说这不是问题的最佳解决方案,但绝对是快速解决方案