使用PHP获取字符串的一部分

时间:2015-11-03 20:22:44

标签: php regex substring explode

我正在阅读RSS源并在页面上输出它,我需要获取<description>标记的子字符串并将其存储为变量(然后转换为不同的时间格式,但我可以自己解决这个问题)。以下是我正在使用的数据样本:

<description>&lt;b&gt;When:&lt;/b&gt; Tuesday, November 03, 2015 - 6:00 PM - 8:00 PM&lt;br&gt;&lt;b&gt;Where:&lt;/b&gt; Adult Literacy Classroom (Lower Level) dedicated in honor of Eleanor Moore&lt;br&gt;&lt;br&gt;Clases de preparaci&#243;n para el GED &#150; grupos de estudio para ayudar con sus habilidades y preparaci&#243;n para obtener su diploma de equivalencia de escuela. Las clases se llevaran a cabo en espa&#241;ol, seg&#250;n la materia (escritura, literatura, estudios sociales, ciencias, matem&#225;ticas y la constituci&#243;n) &lt;br /&gt;&lt;br /&gt;GED preparation classes &#150; Study groups to help build your skills that will prepare you to get your high school equivalency diploma. Classes are taught in Spanish by subject area (writing, literature, social studies, science, math and the constitution)&lt;br /&gt;</description>

我已经将description标记中的所有内容都变为varible,我只需要抓取字符串Tuesday, November 03, 2015 - 6:00 PM - 8:00 PM,但我无法弄清楚如何做到这一点。我有一种感觉PHP的explode可能有用,但我对正则表达式很糟糕。我将继续努力并回复我的进展,但任何帮助将不胜感激。

顺便说一下,我正在使用这种方法来获取数据:http://bavotasan.com/2010/display-rss-feed-with-php/

感谢@ Bomberis123,我能够完成我所需要的工作。我的代码可能有点乱,但我想我会分享给需要做类似事情的人:

<?php
$next_up_at_rss_feed = new DOMDocument();
$next_up_at_rss_feed->load("http://host7.evanced.info/waukegan/evanced/eventsxml.asp?ag=&et=&lib=0&nd=30&feedtitle=Waukegan+Public+Library%3CBR%3ECalendar+of+Programs+%26+Events&dm=rss2&LangType=0");
$next_up_at_posts = array();
foreach ($next_up_at_rss_feed->getElementsByTagName("item") as $node) {
    $date = preg_match("/((\s)([^\<])+)/", $node->getElementsByTagName("description")->item(0)->nodeValue, $matches, PREG_OFFSET_CAPTURE, 3);
    $date = $matches[0][0];
    $next_up_at_post = array (
        "title" => $node->getElementsByTagName("title")->item(0)->nodeValue,
        "date" => $date,
        "link" => $node->getElementsByTagName("guid")->item(0)->nodeValue,
    );
    array_push($next_up_at_posts, $next_up_at_post);
}
$next_up_at_limit = 4;
for ($next_up_at_counter = 0; $next_up_at_counter < $next_up_at_limit; $next_up_at_counter++) {
    // get each value from the array;
    $title = str_replace(" & ", " &amp; ", $next_up_at_posts[$next_up_at_counter]["title"]);
    $link = $next_up_at_posts[$next_up_at_counter]["link"];
    $date_raw = $next_up_at_posts[$next_up_at_counter]["date"];

    // seperate out the date so it can be formatted
    $date_array = explode(" - ", $date_raw);

    // set up various formats for date
    $date = $date_array[0];
    $date_time = strtotime($date);
    $date_iso = date("Y-m-d", $date_time);
    $date_pretty = date("F j", $date_time);

    // set up various formats for start time
    $start = $date_array[1];
    $start_time = strtotime($start);
    $start_iso = date("H:i", $start_time);
    $start_pretty = date("g:ia", $start_time);

    // set up various formats for end time
    $end = $date_array[2];
    $end_time = strtotime($end);
    $end_iso = date("H:i", $end_time);
    $end_pretty = date("g:ia", $end_time);

    // display the data
    echo "<article class='mini-article'><header class='mini-article-header'>";
    echo "<h6 class='mini-article-heading'><a href='{$link}' target='_blank'>{$title}</a></h6>";
    echo "<p class='mini-article-sub-heading'><a href='{$link}' target='_blank'><time datetime='{$date_iso}T{$start_iso}-06:00'>{$date_pretty}, {$start_pretty} - {$end_pretty}</time></a></p>";
    echo "</header></article>";
}
?>

3 个答案:

答案 0 :(得分:2)

尝试使用此正则表达式,您可以使用php regex并使用第一组https://regex101.com/r/fI8nU9/1

$subject = "<description>&lt;b&gt;When:&lt;/b&gt; Tuesday, November 03, 2015 - 6:00 PM - 8:00 PM&lt;br&gt;&lt;b&gt;Where:&lt;/b&gt; Adult Literacy Classroom (Lower Level) dedicated in honor of Eleanor Moore&lt;br&gt;&lt;br&gt;Clases de preparaci&#243;n para el GED &#150; grupos de estudio para ayudar con sus habilidades y preparaci&#243;n para obtener su diploma de equivalencia de escuela. Las clases se llevaran a cabo en espa&#241;ol, seg&#250;n la materia (escritura, literatura, estudios sociales, ciencias, matem&#225;ticas y la constituci&#243;n) &lt;br /&gt;&lt;br /&gt;GED preparation classes &#150; Study groups to help build your skills that will prepare you to get your high school equivalency diploma. Classes are taught in Spanish by subject area (writing, literature, social studies, science, math and the constitution)&lt;br /&gt;</description>";
$pattern = '/((\s)([^&])+)/';
preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE, 3);
echo $matches[0][0];

答案 1 :(得分:1)

Hurray,我能帮助的东西和我的第一个StackOverflow答案!尝试这样的事情。它确实使用正则表达式,但只需要几个简单的语法片段即可。

$data = "<description>&lt;b&gt;When:&lt;/b&gt; Tuesday, November 03, 2015 - 6:00 PM - 8:00 PM&lt;br&gt;&lt;b&gt;Where:&lt;/b&gt; Adult Literacy Classroom (Lower Level) dedicated in honor of Eleanor Moore&lt;br&gt;&lt;br&gt;Clases de preparaci&#243;n para el GED &#150; grupos de estudio para ayudar con sus habilidades y preparaci&#243;n para obtener su diploma de equivalencia de escuela. Las clases se llevaran a cabo en espa&#241;ol, seg&#250;n la materia (escritura, literatura, estudios sociales, ciencias, matem&#225;ticas y la constituci&#243;n) &lt;br /&gt;&lt;br /&gt;GED preparation classes &#150; Study groups to help build your skills that will prepare you to get your high school equivalency diploma. Classes are taught in Spanish by subject area (writing, literature, social studies, science, math and the constitution)&lt;br /&gt;</description>";
$regex = "~<description>&lt;b&gt;When:&lt;/b&gt; (.+?)&lt;br&gt;&lt;b&gt;Where:&lt;/b&gt;~";
preg_match($regex,$data,$match);
echo $match[1];

我对此进行了测试,但确实有效。

在这种情况下,您只需将$regex设置为您希望原始字符串的样子,两端都有~,而(.+?)则要提取的部分是MediaPlayer.create( getApplicationContext(), R.raw.sound1 )

答案 2 :(得分:0)

我远不是regexp的专家,但这可能适合更偏执的程序员:

$s = '<description>&lt;b&gt;When:&lt;/b&gt; Tuesday, November 03, 2015 - 6:00 PM - 8:00 PM&lt;br&gt;&lt;b&gt;Where:&lt;/b&gt; Adult Literacy Classroom (Lower Level) dedicated in honor of Eleanor Moore&lt;br&gt;&lt;br&gt;Clases de preparaci&#243;n para el GED &#150; grupos de estudio para ayudar con sus habilidades y preparaci&#243;n para obtener su diploma de equivalencia de escuela. Las clases se llevaran a cabo en espa&#241;ol, seg&#250;n la materia (escritura, literatura, estudios sociales, ciencias, matem&#225;ticas y la constituci&#243;n) &lt;br /&gt;&lt;br /&gt;GED preparation classes &#150; Study groups to help build your skills that will prepare you to get your high school equivalency diploma. Classes are taught in Spanish by subject area (writing, literature, social studies, science, math and the constitution)&lt;br /&gt;</description>';
$a = array();
$p = '/(Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday),\s'
    .'(January|February|March|April|May|June|July|August|September|October|November|December)\s'
    .'[0-3][0-9],\s[1-2][0-9]{3}\s-\s'    // Year 
    .'[0-2]?[0-9]:[0-5][0-9]\s[AP]M\s-\s' // Time
    .'[0-2]?[0-9]:[0-5][0-9]\s[AP]M/';    // Time 
preg_match( $p, $s, $a, PREG_OFFSET_CAPTURE );
echo $a[0][0];

经过测试和工作......

这将捕获按照描述格式化的日期,在文本的某处。