如何使用 substr 在不同位置使用两个字符串来获取标题
How do I get the title using substr with two string in different position
示例标题:动漫标题:第 01 集,第 01 集字幕
您好,我正在尝试获取标题中的 "Episode 01",但我在使用 substr() 函数时遇到了问题,我该如何在其上声明命令?
$Updated = get_the_title();
if ( strpos( $Updated , ":" ) && ( strripos( $Updated, "," ) ) ) {
// this is the line I'm having trouble to deal with
$Updated = substr( $Updated , strpos( $Updated , ":" ) + 1 );
} else if ( strpos( $Updated , ":" ) ) {
$Updated = substr( $Updated , strpos( $Updated , ":" ) + 1 );
}
从技术上讲,这并不是真正的 WP 问题,可能应该在 PHP 或一般编程论坛上提问。
从您提供的代码中我可以了解到,您认为冒号总是存在的:
,有时可能会有逗号,
,您可能需要看一下expode()
rather than substr()
+ strpos()
.
首先,要回答您的问题,您还需要逗号的位置,这样您就可以告诉 substr()
在何处停止。
$updated = get_the_title();
// calculate the string positions once rather than multiple times
// first colon
$colon_pos = strpos( $updated, ':' );
// first comma AFTER the colon
$comma_pos = strpos( $updated, ',', $colon_pos );
// MUST compare strpos values to false, it can return 0 (zero) which is falsy
if ( $colon_pos !== false && $comma_pos !== false ) {
// start from the colon position plus 1
// use comma position as the length, since it is based on the offset of the colon
$updated = substr( $updated, $colon_pos + 1, $comma_pos );
} else if ( $colon_pos !== false ) {
$updated = substr( $updated, $colon_pos + 1 );
}
如开头所述,这一切都可以简化为 explode()
:
// - first, split the title on the first colon ':', the second/last item of
// that action will be everything after the colon if there is a colon, or
// the whole title if there is no colon
// - second, grab that last item and split it on commas, the first/zeroth
// item of that action will be the episode
// - finally, trim off the excess whitespace
$updated = explode( ':', get_the_title(), 2 );
$updated = trim( explode( ',', end( $updated ) )[0] );
长格式:
$updated = get_the_title(); // the full title string
$updated = explode( ':', $updated, 2 ); // split it in two around the first ':'
$updated = end( $updated ); // grab the last element of the split
$updated = explode( ',', $updated ); // split the remainder on ','
$updated = trim( $updated[0] ); // get the first item after the split and remove excess whitespace
我希望这不会太混乱。
您可以像这样使用辅助函数:
function.php
:
function get_string_between($string, $start, $end){
$string = " ".$string;
$ini = strpos($string,$start);
if ($ini == 0) return "";
$ini += strlen($start);
$len = strpos($string,$end,$ini) - $ini;
return substr($string,$ini,$len);
}
template_file.php
:
$Updated = get_the_title();
$Updated = get_string_between($Updated , ":", ",");
echo $Updated;
示例标题:动漫标题:第 01 集,第 01 集字幕
您好,我正在尝试获取标题中的 "Episode 01",但我在使用 substr() 函数时遇到了问题,我该如何在其上声明命令?
$Updated = get_the_title();
if ( strpos( $Updated , ":" ) && ( strripos( $Updated, "," ) ) ) {
// this is the line I'm having trouble to deal with
$Updated = substr( $Updated , strpos( $Updated , ":" ) + 1 );
} else if ( strpos( $Updated , ":" ) ) {
$Updated = substr( $Updated , strpos( $Updated , ":" ) + 1 );
}
从技术上讲,这并不是真正的 WP 问题,可能应该在 PHP 或一般编程论坛上提问。
从您提供的代码中我可以了解到,您认为冒号总是存在的:
,有时可能会有逗号,
,您可能需要看一下expode()
rather than substr()
+ strpos()
.
首先,要回答您的问题,您还需要逗号的位置,这样您就可以告诉 substr()
在何处停止。
$updated = get_the_title();
// calculate the string positions once rather than multiple times
// first colon
$colon_pos = strpos( $updated, ':' );
// first comma AFTER the colon
$comma_pos = strpos( $updated, ',', $colon_pos );
// MUST compare strpos values to false, it can return 0 (zero) which is falsy
if ( $colon_pos !== false && $comma_pos !== false ) {
// start from the colon position plus 1
// use comma position as the length, since it is based on the offset of the colon
$updated = substr( $updated, $colon_pos + 1, $comma_pos );
} else if ( $colon_pos !== false ) {
$updated = substr( $updated, $colon_pos + 1 );
}
如开头所述,这一切都可以简化为 explode()
:
// - first, split the title on the first colon ':', the second/last item of
// that action will be everything after the colon if there is a colon, or
// the whole title if there is no colon
// - second, grab that last item and split it on commas, the first/zeroth
// item of that action will be the episode
// - finally, trim off the excess whitespace
$updated = explode( ':', get_the_title(), 2 );
$updated = trim( explode( ',', end( $updated ) )[0] );
长格式:
$updated = get_the_title(); // the full title string
$updated = explode( ':', $updated, 2 ); // split it in two around the first ':'
$updated = end( $updated ); // grab the last element of the split
$updated = explode( ',', $updated ); // split the remainder on ','
$updated = trim( $updated[0] ); // get the first item after the split and remove excess whitespace
我希望这不会太混乱。
您可以像这样使用辅助函数:
function.php
:
function get_string_between($string, $start, $end){
$string = " ".$string;
$ini = strpos($string,$start);
if ($ini == 0) return "";
$ini += strlen($start);
$len = strpos($string,$end,$ini) - $ini;
return substr($string,$ini,$len);
}
template_file.php
:
$Updated = get_the_title();
$Updated = get_string_between($Updated , ":", ",");
echo $Updated;