繁体   English   中英

如何在php上过滤$值

[英]How to filter $ value on php

我想过滤值

示例: 11201: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 并将其放入 function.php

<?php
$text = '$duration';
$text = substr($text, 0, 3);
echo $text =str_replace("1","01:", $text);
?>

但总是显示解析错误。

我在此代码片段中注意到两个问题:

<?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";

你可以这样做,静态地:

<?php
$duration = "112";
$exploded = str_split($duration);
$exploded[0] = "0".$exploded[0].":";

$final = implode($exploded);
echo $final;
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM