I need to replace specific value in a dynamic string by preg_replace

68
May 24, 2021, at 01:30 AM

I have html strings such as

$string1 = '<a href="././"><img src="./images/userid=22&u_size=150"/></a>';
$string2 = '<a href="././"><img src="./images/userid=15&u_size=120"/></a>';
$string3 = '<a href="././"><img src="./images/userid=17&u_size=80"/></a>';

If you notice u_size is different in each string. So I need a preg_replace code to change them all for example 150.

so, desired output is

$string1 = '<a href="././"><img src="./images/userid=22&u_size=150"/></a>';
$string2 = '<a href="././"><img src="./images/userid=15&u_size=150"/></a>';
$string3 = '<a href="././"><img src="./images/userid=17&u_size=150"/></a>';
Answer 1

Use a regex for that.

This will find u_size followed by = followed by at least one number of digits.

Then it is flexibly replaced. $1 is the the first found term u_size.

$2 would be the size, so 150, 120, 80. But this is not used here.

$string1 = '<a href="././"><img src="./images/userid=22&u_size=150"/></a>';
$string2 = '<a href="././"><img src="./images/userid=15&u_size=120"/></a>';
$string3 = '<a href="././"><img src="./images/userid=17&u_size=80"/></a>';
$string1 = preg_replace('/(u_size)=(\d+)/', '$1=150', $string1);
$string2 = preg_replace('/(u_size)=(\d+)/', '$1=150', $string2);
$string3 = preg_replace('/(u_size)=(\d+)/', '$1=150', $string3);
echo $string1, PHP_EOL;
echo $string2, PHP_EOL;
echo $string3, PHP_EOL;
<a href="././"><img src="./images/userid=22&u_size=150"/></a>
<a href="././"><img src="./images/userid=15&u_size=150"/></a>
<a href="././"><img src="./images/userid=17&u_size=150"/></a>
Answer 2

This should do it.

$regex = '/(u_size)=(\d+)/';
preg_replace($regex, '$1=150', $string);
Answer 3

Use this regex preg_replace('/u_size=\d+/', 'u_size=150', $string);

Answer 4

You could try something like:

$parts = explode('u_size', $string1);
if (isset($parts[0])) {
    $string1 = $parts[0] . 'u_size=150"/></a>';
} 
Answer 5

Use substr(string, start, end), cut the string from beginning (start 0), until first occurrence of keyword u_size. and concatenate it with fixed part which is u_size=150. Wrapped up all these instructions inside a function same_size and call it whenever and whenever you want.

function same_size($string){
  $till = strpos($string, 'u_size');
  $fixed = 'u_size=150"/></a>';
  return substr($string, 0, $till).$fixed;
}
//Strings:
$string1 = '<a href="././"><img src="./images/userid=22&u_size=150"/></a>';
$string2 = '<a href="././"><img src="./images/userid=15&u_size=120"/></a>';
$string3 = '<a href="././"><img src="./images/userid=17&u_size=80"/></a>';
//Tests:
echo same_size($string1);
echo same_size($string2);
echo same_size($string3);
//Results:
<a href="././"><img src="./images/userid=22&u_size=150"/></a>
<a href="././"><img src="./images/userid=15&u_size=150"/></a>
<a href="././"><img src="./images/userid=17&u_size=150"/></a>
READ ALSO
why UNION ALL command in mysql doesn&#39;t give back any results?

why UNION ALL command in mysql doesn't give back any results?

I am trying to merge two queries into one, but UNION is not working for meHere is the code:

47
wp_enqueue_script was called incorrectly

wp_enqueue_script was called incorrectly

I am not a developer, but I have a theme that is giving me the error wp_enqueue_script was called incorrectlyScripts and styles should not be registered or enqueued until the wp_enqueue_scripts, admin_enqueue_scripts, or login_enqueue_scripts hooks

48
How to limit decimals digits?

How to limit decimals digits?

I am doing a basic calculator using double, but I wanted the output to limit on showing only 2 decimal digits

73
How Scroll particular in DIV section with browser&#39;s main horizontal scrollbar

How Scroll particular in DIV section with browser's main horizontal scrollbar

The scroll will work in the div section but not will show scroll in the div section but this scroll shows out of the body like browser window scroll in horizontal

58