-3

Ok I found an answer PHP - split a string of HTML attributes into an indexed array

Thank's

I wanna replace every white space character between quotes with %20

Example:

<input type="text" title="this is a fish">

Desired result:

<input type="text" title="this%20is%20a%20fish">

Another example

$_POST['foo'] = '<input type="text" title="this is a fish">';
$parsed = '<input type="text" title="this%20is%20a%20fish">';

As seen I only whant to replace the spaces within the qoutes, not any other. So str_replace simply will not help here

The desierd end result of this is an array of the parameters

This is what I did

<?php
$tag_parsed = trim($tag_parsed);
$tag_parsed = str_replace('"', '', $tag_parsed);
$tag_parsed = str_replace(' ', '&', $tag_parsed);
parse_str($tag_parsed, $tag_parsed);

But when a parameter has a space, it breaks.

Community
  • 1
  • 1
  • output->foo o – varunkumar Jan 16 '16 at 22:55
  • @DenisAlexandrov that is not what I'm asking, I have a string like in the example, I don't want to replace every white-space, just the ones in the qoutes – Mordi Sacks Jan 16 '16 at 23:01
  • @MordiSacks, however, the functions' are same. You just have to properly use them. Example: `` – Denis Alexandrov Jan 16 '16 at 23:04
  • Again, I don't create the string, I receive it, so I cannot call a function within it – Mordi Sacks Jan 16 '16 at 23:06
  • Could you update your questions with this process, so we could have an idea on what's going on? – Denis Alexandrov Jan 16 '16 at 23:08
  • @MordiSacks check my update – Denis Alexandrov Jan 16 '16 at 23:29
  • Did anybody find a real answer to the original question? "I wanna replace every white space character between quotes with %20" . The "real" answer should work regardless of the string being an HTML tag or not. In my case, I have this string (i put it in brackets to prevent confusion with qoutes): (* LIST (\HasNoChildren \UnMarked) "/" "Posta indesiderata") Items are sperated by spaces, but a space also appears in last item, so how do I properly separate the items so I get 6 items rather than 7? Maybe I should open an other question? – jumpjack Feb 20 '19 at 08:31

1 Answers1

0

UPDATE

Basing on your last comments, it seems you need something like this:

$str = '<input type="text" title="this is a fish">';
preg_match('/title="(.*)"/', $str, $title);
$parsed_title = str_replace(' ', '%20', $title[1]);

But it seems there's something could be done to improve tre rest of the code.


You have to use urlencode or similiar function:

$str = "Your spaced string";
$new = urlencode($str); //Your%20spaced%20string

Or, use preg_replace:

$str = "Your spaced string";
$new = preg_replace("/\s+/", "%20", $str);

OR, without regexp:

$new = str_replace(" ", "%20", $str);