需要删除“src=”来自所有 PHP 数组值

发布于 2024-12-28 10:34:26 字数 1330 浏览 2 评论 0原文

可能的重复:
如何使用 PHP 解析和处理 HTML?

以下是我正在尝试执行以下操作:

1.)从我的 WordPress 博客文章中查找所有图像

2.)在帖子顶部创建指向其图像 URL 的链接

3.)为每个链接指定属性:

rel="prettyPhoto[ INSERT POST TITLE HERE ]"

下面是我所拥有的远:

$szPostContent = $post->post_content;
$szSearchPattern = '@src="([^"]+)"@';

// Run preg_match_all to grab all the images and save the results in $aPics
preg_match_all( $szSearchPattern, $szPostContent, $aPics );


// Check to see if we have at least 1 image src
$iNumberOfPics = count($aPics[0]);


if ( $iNumberOfPics > 0 ) {

///this is what I want to do
/// $aPicsTRIMMED = preg_replace('/src=/','/http=/',$aPics);

 for ( $i=0; $i < $iNumberOfPics ; $i++ ) {
echo ' <a rel="prettyPhoto['.$portTit.']"';
      echo $aPics[0][$i];
echo '>Image</a>';

     };
};

现在,照原样,这段代码让我得到以下结果:

<a rel="prettyPhoto[Correct Post Title]" src="http://mydomain.com/myimage.jpg">Image</a>

非常接近,但我需要 href 而不是 src。如果您看到底部的注释,我一直在尝试使用 preg 替换(错误地),并且不确定这是否朝着正确的方向发展。这是我第一次接触 PHP,但遇到了困难。希望更熟悉的人能够在我之前解决这个问题......(如果我做到了,我会发布成功故事)同时请在我把自己逼疯之前帮忙。

谢谢

Possible Duplicate:
How to parse and process HTML with PHP?

Here is what I'm trying to do:

1.)Find all images from my wordpress blog posts

2.)Create links to their image URLs at the top of the post

3.)Give each of these links the attribute:

rel="prettyPhoto[ INSERT POST TITLE HERE ]"

Below is what I have so far:

$szPostContent = $post->post_content;
$szSearchPattern = '@src="([^"]+)"@';

// Run preg_match_all to grab all the images and save the results in $aPics
preg_match_all( $szSearchPattern, $szPostContent, $aPics );


// Check to see if we have at least 1 image src
$iNumberOfPics = count($aPics[0]);


if ( $iNumberOfPics > 0 ) {

///this is what I want to do
/// $aPicsTRIMMED = preg_replace('/src=/','/http=/',$aPics);

 for ( $i=0; $i < $iNumberOfPics ; $i++ ) {
echo ' <a rel="prettyPhoto['.$portTit.']"';
      echo $aPics[0][$i];
echo '>Image</a>';

     };
};

Right now, as is, this code gets me the following:

<a rel="prettyPhoto[Correct Post Title]" src="http://mydomain.com/myimage.jpg">Image</a>

very close but I need href instead of src. If you see commented out at the bottom I've been trying to use preg replace (incorrectly) and not sure if this is moving in the right direction or not. This is my first go at PHP and I've hit a wall. Hopefully someone more familiar will be able to figure this out before I can... (will post success story if I make it there) In the mean time PLEASE HELP before I drive myself crazy.

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情话已封尘 2025-01-04 10:34:26

替换:

echo $aPics[0][$i];

为:

echo str_replace('src=', 'href=', $aPics[0][$i]);

Replace:

echo $aPics[0][$i];

With:

echo str_replace('src=', 'href=', $aPics[0][$i]);
远昼 2025-01-04 10:34:26

您正在使用此行注入整个匹配 src=...

      echo $aPics[0][$i];

但您只应该使用 ye 正则表达式中的内部匹配组 ([^"]+) 。该值可在匹配结果数组的 [1] 中找到:

      echo ' href="' . $aPics[1][$i] . '"';

它只需要被新的属性片段 href="..." 包围即可。

You are injecting the whole match src=... with this line:

      echo $aPics[0][$i];

But you only should be using the inner match group ([^"]+) from ye regex. That one is available in [1] of the match result array:

      echo ' href="' . $aPics[1][$i] . '"';

And it just needs to be surrounded by the new attribute snippets href="...".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文