如何筛选在PHP
问题描述:
$价值我想筛选值如何筛选在PHP
例如: 112
是01:12
从脚本
$duration = get_post_meta($post->ID, 'TMDB_Runtime', true);
if (! empty($duration)) {
echo '<div class="gmr-duration-item" property="duration">' . $duration . __(' <a class="fa fa-clock-o" aria-hidden="true"></a>','movies') . '</div>';
我试着使用SUBSTR和str_replace函数,并把它into function.php
<?php
$text = '$duration';
$text = substr($text, 0, 3);
echo $text =str_replace("1","01:", $text);
?>
但是总是显示解析错误。
答
你可以做这种方式,静态:
<?php
$duration = "112";
$exploded = str_split($duration);
$exploded[0] = "0".$exploded[0].":";
$final = implode($exploded);
echo $final;
?>
答
有两个问题,我在此代码段中注意到:
<?php
$text = '$duration';
$text = substr($text, 0, 3);
echo $text =str_replace("1","01:", $text);
?>
第一: echo $text =str_replace("1","01:", $text);
是不正确的语法。您不能将函数分配给函数。将它分成两行。
$text = str_replace("1","01:", $text);
echo $text;
二: $text = '$duration';
不设置$text
到112
。它将它设置为文字字符串$duration
。 PHP中的单引号不会评估变量,这就是双引号。所以,试试这个来代替:
$text = "$duration";
+0
是的,我知道,在'$ duration'应该是'112'和'回声$文字= str_replace(“1”,“01:”,$ text);'应该是'01:01:2'这只是一个例子,因为我已经不知道 –
谢谢老兄:)。这是真正的方法,它的工作,但是...''持续时间'它并不总是'112'值,我的意思是,从$持续时间(eee)获得价值并将其过滤到(ee:ee) –
但它会一直有1,2,3或4个数字是一个时间。所以你可以首先检查字符串长度,然后有一个switch语句,如果它是1,2,3或4,应用0:#或#:#等等 – clearshot66