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.)

 Answers

4
$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
4

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
 
1

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/src/index.php
/var/www/src/more_source.php
/var/www/clients/client_a/settings.php
/var/www/clients/client_a/src -> ../../src/
/var/www/clients/client_b/settings.php
/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
 
fjarlq
 
3

There is a string method called zfill:

>>> '12344'.zfill(10)
0000012344

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
2

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

Friday, December 9, 2022
 
kalpesh
 
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 :