Forum Moderators: coopster

Message Too Old, No Replies

preg replace question.

         

Blooish

7:37 pm on Mar 8, 2008 (gmt 0)

10+ Year Member



I need to change spaces to $nbsp; only inside the values of the attributes of some tags. For example <a href="./One script.html"></a> should become <a href="./One&nbsp;script.html"></a>.
I tried to use preg_replace('/="\s\s+"/i','&nbsp;',$string) but it doesnt seem to work.
Any Help is appreciated!

cameraman

8:16 pm on Mar 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Someone else can probably come up with a better one but this looks like it's working:
#(a href[^\s]+)([\s])#

Blooish

1:34 pm on Mar 9, 2008 (gmt 0)

10+ Year Member



yeah, but i want to change spaces in all attributes of a tag, not only href.

coopster

6:36 pm on Mar 10, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Why? I mean, what is the intended use here? If that href is really a URL you should consider rawurlencode [php.net]. For everything else, perhaps you should consider htmlentities [php.net].

Blooish

1:17 pm on Mar 11, 2008 (gmt 0)

10+ Year Member



coopster, you didn't understand my question. I needed to create a script to exctract url from a webpage and its attributes. I found a solution,but sometimes it doesnt exctract url, for example for google <a href=somelink></a> ,coz my script looks for <a href="somelink"></a>.
Anyway thanks for your attention.

[edited by: coopster at 6:21 pm (utc) on Mar. 11, 2008]
[edit reason] no urls please TOS [webmasterworld.com] [/edit]

coopster

6:48 pm on Mar 11, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



First you said you needed to replace certain entities. Now you are saying you want to extract. Indeed, I am not understanding your question ;)

If you are trying to locate links in a web page, have you searched the forums here yet? There are many threads here that demonstrate how to do so with many examples of regular expressions.