0

i want to replace every space in a link with %20.

<a href="Replace the spaces here">Some text</a>

i want to get This:

<a href="Replace%20the%20spaces%20here">Some text</a>

not this:

<a%20href="Replace%20the%20spaces%20here">Some%20text</a>

How to do this? preg_replace? Solution (because I cant post an answer):

$search= '(href="(.*?)")s';
$replace= '';
$newstring= preg_replace_callback($search,create_function('$treffer','urlencode($treffer[0]);'),$string);
Lajos Veres
  • 13,595
  • 7
  • 43
  • 56
  • Can you show us your code? – Pitchinnate Nov 22 '13 at 19:53
  • 1
    Why do you have space-less links in the first place? How is this page being generated? – Pekka Nov 22 '13 at 19:53
  • I would agree with @Pekka웃 here. It is REALLY bad practice to have URL's with spaces in them in the first place. – Mike Brant Nov 22 '13 at 19:55
  • 1
    I don't see the purpose of doing this. Modern day browsers do this automatically, unless your users and/or you are using a dinosaur browser. Plus, having files with spaces to start off with, is really bad practice. I suggest you start naming your files using underscores, and/or hyphens. – Funk Forty Niner Nov 22 '13 at 19:58
  • What's wrong with file names with spaces? As long as they are properly escaped? – Pekka Nov 22 '13 at 20:05
  • *"As long as they are properly escaped"* - It's an extra step that can be avoided in the first place, but that's just me ;-) @Pekka웃 – Funk Forty Niner Nov 22 '13 at 20:07

3 Answers3

3

You should use the urlencode() function for the href part.

http://php.net/urlencode

Salman A
  • 262,204
  • 82
  • 430
  • 521
Lajos Veres
  • 13,595
  • 7
  • 43
  • 56
  • Seeing his current result, I suspect he's already doing that. – Denis de Bernardy Nov 22 '13 at 19:52
  • 1
    This is unlikely to help - you can't just apply `urlencode()` to the HTML code above. – Pekka Nov 22 '13 at 19:57
  • If you're in PHP, you should definitely be using the existing encode and decode functions on your URLs (even if they don't have spaces). – Preston Badeer Nov 22 '13 at 19:57
  • 1
    @prrstn true, but how would you apply `urlencode()` to the HTML code the OP shows? It's not a valid answer to the OP's situation – Pekka Nov 22 '13 at 19:58
  • _... for the *href* part_..., sounds OK to me. – Salman A Nov 22 '13 at 19:59
  • @SalmanA true, but by that standard an answer saying "to solve this problem, you need to use PHP." is also OK – Pekka Nov 22 '13 at 20:03
  • I think this answer matches the question's precision. At least this is why I posted. Anyway we are guessing what the question's author have tried already. – Lajos Veres Nov 22 '13 at 20:05
  • So why not ask, and wait, for clarification instead of posting a potentially (likely) worthless answer... – Pekka Nov 22 '13 at 20:06
  • But i want to find the part from href=" to " out of a string, and then do the urlencode() – Basti Destruction Nov 22 '13 at 20:08
  • @Pekka웃 I assumed this can help. And if not the author clarifies why not. – Lajos Veres Nov 22 '13 at 20:09
  • 1
    @qwertz in that case, you'll need a HTML parser. [How do you parse and process HTML/XML in PHP?](http://stackoverflow.com/q/3577641) – Pekka Nov 22 '13 at 20:10
  • @Pekka웃 The OP stated "How to do this? preg_replace?" which implies he/she is using PHP in the HTML (via tags or something). That is why I answered with PHP. It's easy to do . – Preston Badeer Nov 22 '13 at 20:12
2

For the note, every modern browser will process the following just fine:

<a href="Replace the spaces here">Some text</a>

If you insist in doing it regardless, and assuming you cannot urlencode() the links before they're output, you need to use either of:

  • preg_replace_callback() or
  • a DOM parser

Using either will allow you to only apply urlencode() where it's needed.

Denis de Bernardy
  • 75,850
  • 13
  • 131
  • 154
0

You could try this:

<a href="<? echo(urlencode("Replace the spaces here")); ?>">Some text</a>
Chris
  • 458
  • 4
  • 17
  • Yes, i want to find the part from href=" to " out of a string, and then do the urlencode() – Basti Destruction Nov 22 '13 at 20:07
  • @qwertz consider responding to our requests for clarification then – Pekka Nov 22 '13 at 20:07
  • 1
    @qwertz1029384756 So, are you saying you are parsing existing html rather than writing a new page? (Incidentally I cannot yet ask for clarification on the main question itself.) – Chris Nov 22 '13 at 20:13