php - Replace every white space character between quotes -


ok found answer php - split string of html attributes indexed array

thank's

i wanna replace every white space character between quotes %20

example:

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

desired result:

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

another example

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

as seen whant replace spaces within qoutes, not other. str_replace not here

the desierd end result of array of parameters

this 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 parameter has space, breaks.

update

basing on last comments, seems need this:

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

but seems there's done improve tre rest of code.


you have 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); 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -