Php preg_match_all,wordpress函数
我在网上找到了这段代码…我真的不懂php。。。 有人能给我技术吗Php preg_match_all,wordpress函数,php,preg-match-all,Php,Preg Match All,我在网上找到了这段代码…我真的不懂php。。。 有人能给我技术吗 <?php function catch_that_image() { global $post, $posts; $first_img = ''; ob_start(); ob_end_clean(); $output = preg_match_all('<img.+src=[\'"]([^\'"]+)[\'"].*>', $post->post_content, $matches);
<?php
function catch_that_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('<img.+src=[\'"]([^\'"]+)[\'"].*>', $post->post_content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){ //Defines a default image
$first_img = "/images/default.jpg";
}
return $first_img;
}
?>
我希望输出采用a-z或符号或数字,在此=之后,在此之前]
谢谢你在网上找到的代码有点不相关。为了实现你想要的,你需要这样的东西: 在那之后$name将绑定到ubuntududio-tribal-54 有关PHP的详细信息,请参阅文档。
另请参阅,这是一个关于一般情况的信息来源。自学如何?我不确定我是否了解。你的图片是短码还是怎么回事?为什么在post_内容中要将图像包装在括号中?如果是正确的HTML,我建议使用DOMDocument,而不是regex。当我没有完成任务的技能时,我会雇佣一些人。是的,adeneo,我在post_内容中使用图像短代码……而DOMDocument,我不懂这个编码……你能帮我处理这个代码吗?你为什么要使用你不懂的代码?不确定你为什么要逃避两次\\[但在PCRE for PHP中,您只需转义一次:\[感谢专家系统..我做了这个,但如何替换$str=[image name=ubuntudio-tribal-54];那么,这个str从wordpress post获取代码?@HamZa DzCyberDeV True,在PCRE for PHP中,您只需转义一次-我已经习惯了Java的双重转义:我将尽快从我的答案中删除双重转义。Thx指出!@Ari Riko我的代码只是一个示例。我无法肯定地回答您应该如何实现它在你的特定场景中,因为我不知道你的代码。也许像这样的东西对你有用:@ExpertSystem是的,它有用..谢谢专家系统…我非常感谢你的帮助,再次,非常感谢专家系统。。。
[image name=ubuntustudio-tribal-54]
or
[image name=office-tool]
or
[image name=car]
or
[image name=158]
$str = "[image name=ubuntustudio-tribal-54] ";
$pat = "~\[image name=([^\]]+)~i";
preg_match($pat, $str, $matches);
$name = $matches[1];