Viewed   127 times

I've got the following source:

<font color="black">0</font><font color="white">1101100001001101</font><font color="black">1</font><font color="white">0110</font>

And would like to replace all white 1 and 0 with spaces. I can match them easily with

/<font color="white">([10]*)</font>/g

Is there a replace pattern (I'm using PHP) to generate the same number of spaces for the matching group $1?

The result should look like this:

<font color="black">0</font><font color="white">                </font><font color="black">1</font><font color="white">    </font>

(And please ignore the fact that I'm parsing HTML with regexs here. I'm more interested in the solution to the regex problem than in the HTML.)


$test = '<font color="black">0</font><font color="white">1101100001001101</font><font color="black">1</font><font color="white">0110</font>';

echo preg_replace ('~(<font color="white">)([10]*)(</font>)~e', '"\1" . str_repeat(" ", strlen ("\2")) . "\3"', $test);
Sunday, September 25, 2022

Assuming you want to remove both ( and ) from the $search string:

$search = preg_replace('/(|)/','',$search);

I think the fastest way to do this is using the strtr function, like this:

$search = strtr($search, array('(' => '', ')' => ''));
Tuesday, November 15, 2022

A easy solution would be to put the source code in a subdirectory, except the files, which should be altered for each client (for example, the config file).

You can then put this source code directory somewhere out and just create symlinks to it.

For example, your directory structure might look like:

/var/www/clients/client_a/src -> ../../src/
/var/www/clients/client_b/src -> ../../src/

If you choose this structure, the only thing you would need to change, would be the include for settings.php (e.g. from require "settings.php" to require "../settings.php").

Sunday, September 11, 2022

There is a string method called zfill:

>>> '12344'.zfill(10)

It will pad the left side of the string with zeros to make the string length N (10 in this case).

Thursday, December 1, 2022

preg_replace('/[_]+/', '_', $your_string);

Friday, December 9, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :