简体   繁体   English

如何用 %20 替换空格<img>标签

[英]How to replace spaces with %20 in <img> tags

I would like to replace all spaces in the image tags of a html text.我想替换 html 文本的图像标签中的所有空格。

Example:例子:

<img src="photo 1.jpg" />

to

<img src="photo%201.jpg"/>

I didn't find a soultion with preg_replace, but it may be a simple regexp line.我没有找到 preg_replace 的灵魂,但它可能是一个简单的正则表达式行。 Thanks!谢谢!

Edit : Sorry guys, my description was not very clear.编辑:对不起,我的描述不是很清楚。 So, I have a full html page and I only want to replace inside the img tags.所以,我有一个完整的 html 页面,我只想在 img 标签内替换。 I can't use urlencode here as it encodes the other stuff as well.我不能在这里使用 urlencode,因为它也会对其他内容进行编码。

EDIT编辑

I may have missed the point, if you are not scanning over already written out HTML, this probably is not for you.我可能错过了重点,如果您没有扫描已经写出的 HTML,这可能不适合您。 This is for if you were scrapping it for whatever reason.这是因为如果您出于任何原因将其报废。 I am leaving it up for pure nostalgia as it could help someone else who wants to scrape and replace.我把它留给纯粹的怀旧之情,因为它可以帮助其他想要刮掉和更换的人。 Sorry for the confusion.对困惑感到抱歉。


Since you know the tags and the attribute, I would suggest looking into the PHP DOM to do this.由于您知道标签和属性,我建议您查看PHP DOM来执行此操作。 Regex can do it, but the DOM would be preferred and perhaps bit easier / more reliable, given the context you are looking at.正则表达式可以做到这一点,但考虑到您正在查看的上下文,DOM 将是首选,并且可能更容易/更可靠。 This will scan the generated HTML and allow you to replace items inside of attributes (in this case src ) that with using the rawurlencode() you can get the spaces converted to %20.这将扫描生成的 HTML 并允许您替换属性内的项目(在本例中为src ),使用rawurlencode()可以将空格转换为 %20。

<?php
$dom = new DOMDocument();
// $dom->load('test.html'); // if in a file
$dom->loadHTML($html); // if in a string

for ($i=0; $i<$dom->getElementsByTagName('img')->length; $i++) {
    $encoded = implode("/", array_map("rawurlencode",
         explode("/", $dom->getElementsByTagName('img')
                    ->item($i)->getAttribute('src'))));

    $dom->getElementsByTagName('img')
            ->item($i)
            ->setAttribute('src',$encoded);
}


echo $dom->saveHTML();

Worked on my small test file, just an example of how it could be done:)处理我的小测试文件,只是一个如何完成的例子:)

The space is represented by a %20 in the url but there are other chars that you might want to have converted for other images so you should use the general urlencode function instead of using a "simple regex" as stated in the OP.该空间由 url 中的 %20 表示,但您可能希望为其他图像转换其他字符,因此您应该使用通用 urlencode function 而不是使用 OP 中所述的“简单正则表达式”。

<img src="<?php echo urlencode('file name.jpg'); ?>"/>

this is what you need rawurlencode();这就是你需要rawurlencode();

php.net/rawurlencode php.net/rawurlencode

Hey, I've found a simple solution, using preg_replace_callback.嘿,我找到了一个简单的解决方案,使用 preg_replace_callback。 I've never heard of this function before, but it's great.我以前从未听说过这个 function,但它很棒。

Posting the code here:在此处发布代码:

$text = preg_replace_callback("/src=[\'\"](.*?)[\'\"]/", "removeSpaces", $text);
function removeSpaces($matches) {
  return "src='" . str_replace(" ", "%20", $matches[1]) . "'";
}

Thanks for all the replies.感谢所有的答复。

Something like the following may work:像下面这样的东西可能会起作用:

$img_src = 'images/some crazy image.jpg';
$img_src = preg_replace('/ /g', '%20', $img_src);
echo '<img src="' . $img_src . '" alt="some image" />';

But it's a bit hard to tell since you didn't give us any details about why preg_replace didn't work, what its output was, etc.但这有点难以判断,因为您没有向我们提供有关preg_replace为何不起作用、其 output 是什么等的任何详细信息。

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

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