使用 PHP 获取字符串的一部分
Get part of a string with PHP
我正在阅读 RSS 提要并将其输出到页面上,我需要获取 <description>
标记的子字符串并将其存储为变量(然后转换为不同的时间格式,但我自己可以弄清楚)。这是我正在处理的数据示例:
<description><b>When:</b> Tuesday, November 03, 2015 - 6:00 PM - 8:00 PM<br><b>Where:</b> Adult Literacy Classroom (Lower Level) dedicated in honor of Eleanor Moore<br><br>Clases de preparación para el GED – grupos de estudio para ayudar con sus habilidades y preparación para obtener su diploma de equivalencia de escuela. Las clases se llevaran a cabo en español, según la materia (escritura, literatura, estudios sociales, ciencias, matemáticas y la constitución) <br /><br />GED preparation classes – 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)<br /></description>
我已经将描述标签中的所有内容都作为变量,我只需要获取字符串 Tuesday, November 03, 2015 - 6:00 PM - 8:00 PM
,但我不知道该怎么做。我觉得 PHP 的 explode
可能有用,但我对正则表达式很糟糕。我会继续努力,post 支持我的进步,但我们将不胜感激任何帮助。
顺便说一句,我正在使用这种方法来获取数据: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(" & ", " & ", $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>";
}
?>
试试这个正则表达式,你可以使用 php 正则表达式并使用第一组 https://regex101.com/r/fI8nU9/1
$subject = "<description><b>When:</b> Tuesday, November 03, 2015 - 6:00 PM - 8:00 PM<br><b>Where:</b> Adult Literacy Classroom (Lower Level) dedicated in honor of Eleanor Moore<br><br>Clases de preparación para el GED – grupos de estudio para ayudar con sus habilidades y preparación para obtener su diploma de equivalencia de escuela. Las clases se llevaran a cabo en español, según la materia (escritura, literatura, estudios sociales, ciencias, matemáticas y la constitución) <br /><br />GED preparation classes – 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)<br /></description>";
$pattern = '/((\s)([^&])+)/';
preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE, 3);
echo $matches[0][0];
万岁,我可以提供帮助,这是我的第一个 Whosebug 答案!尝试这样的事情。它确实使用了正则表达式,但您可以使用一些简单的语法。
$data = "<description><b>When:</b> Tuesday, November 03, 2015 - 6:00 PM - 8:00 PM<br><b>Where:</b> Adult Literacy Classroom (Lower Level) dedicated in honor of Eleanor Moore<br><br>Clases de preparación para el GED – grupos de estudio para ayudar con sus habilidades y preparación para obtener su diploma de equivalencia de escuela. Las clases se llevaran a cabo en español, según la materia (escritura, literatura, estudios sociales, ciencias, matemáticas y la constitución) <br /><br />GED preparation classes – 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)<br /></description>";
$regex = "~<description><b>When:</b> (.+?)<br><b>Where:</b>~";
preg_match($regex,$data,$match);
echo $match[1];
我测试了这个并且它有效。
在这种情况下,您只需将 $regex
设置为您期望原始字符串的样子,在两端设置 ~
,在您想要的部分设置 (.+?)
提取物是。
我远不是正则表达式专家,但这可能适合更偏执的程序员:
$s = '<description><b>When:</b> Tuesday, November 03, 2015 - 6:00 PM - 8:00 PM<br><b>Where:</b> Adult Literacy Classroom (Lower Level) dedicated in honor of Eleanor Moore<br><br>Clases de preparación para el GED – grupos de estudio para ayudar con sus habilidades y preparación para obtener su diploma de equivalencia de escuela. Las clases se llevaran a cabo en español, según la materia (escritura, literatura, estudios sociales, ciencias, matemáticas y la constitución) <br /><br />GED preparation classes – 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)<br /></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];
已测试并正常工作...
这将捕获文本某处所描述格式的日期。
我正在阅读 RSS 提要并将其输出到页面上,我需要获取 <description>
标记的子字符串并将其存储为变量(然后转换为不同的时间格式,但我自己可以弄清楚)。这是我正在处理的数据示例:
<description><b>When:</b> Tuesday, November 03, 2015 - 6:00 PM - 8:00 PM<br><b>Where:</b> Adult Literacy Classroom (Lower Level) dedicated in honor of Eleanor Moore<br><br>Clases de preparación para el GED – grupos de estudio para ayudar con sus habilidades y preparación para obtener su diploma de equivalencia de escuela. Las clases se llevaran a cabo en español, según la materia (escritura, literatura, estudios sociales, ciencias, matemáticas y la constitución) <br /><br />GED preparation classes – 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)<br /></description>
我已经将描述标签中的所有内容都作为变量,我只需要获取字符串 Tuesday, November 03, 2015 - 6:00 PM - 8:00 PM
,但我不知道该怎么做。我觉得 PHP 的 explode
可能有用,但我对正则表达式很糟糕。我会继续努力,post 支持我的进步,但我们将不胜感激任何帮助。
顺便说一句,我正在使用这种方法来获取数据: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(" & ", " & ", $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>";
}
?>
试试这个正则表达式,你可以使用 php 正则表达式并使用第一组 https://regex101.com/r/fI8nU9/1
$subject = "<description><b>When:</b> Tuesday, November 03, 2015 - 6:00 PM - 8:00 PM<br><b>Where:</b> Adult Literacy Classroom (Lower Level) dedicated in honor of Eleanor Moore<br><br>Clases de preparación para el GED – grupos de estudio para ayudar con sus habilidades y preparación para obtener su diploma de equivalencia de escuela. Las clases se llevaran a cabo en español, según la materia (escritura, literatura, estudios sociales, ciencias, matemáticas y la constitución) <br /><br />GED preparation classes – 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)<br /></description>";
$pattern = '/((\s)([^&])+)/';
preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE, 3);
echo $matches[0][0];
万岁,我可以提供帮助,这是我的第一个 Whosebug 答案!尝试这样的事情。它确实使用了正则表达式,但您可以使用一些简单的语法。
$data = "<description><b>When:</b> Tuesday, November 03, 2015 - 6:00 PM - 8:00 PM<br><b>Where:</b> Adult Literacy Classroom (Lower Level) dedicated in honor of Eleanor Moore<br><br>Clases de preparación para el GED – grupos de estudio para ayudar con sus habilidades y preparación para obtener su diploma de equivalencia de escuela. Las clases se llevaran a cabo en español, según la materia (escritura, literatura, estudios sociales, ciencias, matemáticas y la constitución) <br /><br />GED preparation classes – 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)<br /></description>";
$regex = "~<description><b>When:</b> (.+?)<br><b>Where:</b>~";
preg_match($regex,$data,$match);
echo $match[1];
我测试了这个并且它有效。
在这种情况下,您只需将 $regex
设置为您期望原始字符串的样子,在两端设置 ~
,在您想要的部分设置 (.+?)
提取物是。
我远不是正则表达式专家,但这可能适合更偏执的程序员:
$s = '<description><b>When:</b> Tuesday, November 03, 2015 - 6:00 PM - 8:00 PM<br><b>Where:</b> Adult Literacy Classroom (Lower Level) dedicated in honor of Eleanor Moore<br><br>Clases de preparación para el GED – grupos de estudio para ayudar con sus habilidades y preparación para obtener su diploma de equivalencia de escuela. Las clases se llevaran a cabo en español, según la materia (escritura, literatura, estudios sociales, ciencias, matemáticas y la constitución) <br /><br />GED preparation classes – 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)<br /></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];
已测试并正常工作...
这将捕获文本某处所描述格式的日期。