Get ready for BLACK FRIDAY shopping starting in

Forums

PHP

This topic is locked

Using preg_replace to insert spaces

Posted 31 Mar 2008 14:21:45
1
has voted
31 Mar 2008 14:21:45 Matt Bailey posted:
Hi all.

I'm trying to adapt a script I use for cleaning up filenames. Normally it would replace spaces with an underscore character. However, I would like it to allow spaces. I've tried changing the "_" to " ", or "&amp;nbsp;", or "%20". I've also tried finding out what the ascii and hex codes for space are and using these, but couldn't find anything that was clear enough. Nothing is working - " " inserts nothing and all the rest throw up errors. I guess I could leave out the bit of pre_replace that finds the space characters, but I don't know which bit that is. It's all greek (or hex) to me <img src=../images/dmxzone/forum/icon_smile_wink.gif border=0 align=middle>

Can anyone help?

<pre id=code><font face=courier size=2 id=code>
&lt;?php
function clean_title($title) {
$reserved = preg_quote('\/:*?"\'&lt;&gt;|', '/');
return preg_replace("/([\\x00-\\x20\\x7f-\\xff{$reserved}])/e", "_", $title);
}
?&gt;
</font id=code></pre id=code>

___________________________________
"Some people say if you play a windows cd backwards it plays satanic messages. That's nothing, because if you play it forwards it installs windows!"

Edited by - matm00 on 31 Mar 2008 14:22:38

Edited by - matm00 on 31 Mar 2008 14:23:23

Replies

Replied 31 Mar 2008 14:56:40
31 Mar 2008 14:56:40 Matt Bailey replied:
No matter, I've solved it. After a bit more googling I found out that the bit of hex (or is it octal?) for space is '\\x20', so I changed it to '\\x19' (to stop one character before space) and all is working as I'd like it now <img src=../images/dmxzone/forum/icon_smile.gif border=0 align=middle>

___________________________________
"Some people say if you play a windows cd backwards it plays satanic messages. That's nothing, because if you play it forwards it installs windows!"

Reply to this topic