I have a string:
"This is the string. My height="200" and width="200".The second height="300" and width ="300""
How do I scan the string and get the first height element and replace it? Meaning I just want to grab "height="200""
and either replace it or remove it completely but how do I scan the file for just the first occurrence? Also for the actual string I'm using, I don't know what the height is set to so I can't just search for it. I'm thinking I need to find "height="
and get the next few characters after it and change it. I know I can search using:
function str_replace_limit($search, $replace, $string, $limit = 1) {
$pos = strpos($string, $search);
if ($pos === false) {
return $string;
}
$searchLen = strlen($search);
for ($i = 0; $i < $limit; $i++) {
$string = substr_replace($string, $replace, $pos, $searchLen);
$pos = strpos($string, $search);
if ($pos === false) {
break;
}
}
return $string;
}
$search = 'height';
$replace = ' ';
$string = "This is the string. My height="200" and width="200".The second height="300" and width ="300"";
$limit = 1;
$replaced = str_replace_limit(($search), $replace, $string, $limit);
That returns:
This is the string. My ="200" and width="200".The second height="300" and width ="300"
That finds the first height element but I can't get the characters after it? Any ideas? Thanks in advance!
You can easily do this with preg_replace()
and a bit of regex. Using the Limit option of preg_replace()
(the maximum possible replacements for each pattern in each subject string. Defaults to -1 which is no limit) helps you to limit the number of matches returned. Setting the limit to 1 returns the first match:
$subject = 'This is the string. My height="200" and width="200".The second height="300" and width ="300"';
$search = '/(height=\")(\d+)(\")/';
$replace = 'height="450"';
$new = preg_replace($search, $replace, $subject, 1);
echo $new;
Returns:
This is the string. My height="450" and width="200".The second height="300" and width ="300"
The explanation of the regex:
1st Capturing group (height=\")
-- height=
matches the characters height=
literally (case sensitive) -- \"
matches the character " literally
2nd Capturing group (\d+)
-- \d+
match a digit [0-9] -- Quantifier: +
Between one and unlimited times, as many times as possible, giving back as needed [greedy]
3rd Capturing group (\")
-- \"
matches the character "
literally
To test the regex I used regex101.com to make sure the regex shown works. A note for those using regex101.com - always apply the \g modifier (for global matches) as PHP is almost always greedy when using regex.
You are searching for simple text and don't tell your function that it should search something more and how this "more" is looks like. To achive this you need to use a preg_repalce
function descibed here. You can relace your function with something like this
function str_replace_limit($search, $replace, $string, $limit = 1)
{
$pattern = '/' . $search . '="(.*?)"/';
return preg_replace($pattern, $replace, $string, $limit);
}
The needed replacement can be easily performed by using preg_replace
function with the limit
flag(The maximum possible replacements for each pattern in each subject string. Defaults to -1 (no limit).)
$string = 'This is the string. My height="200" and width="200".The second height="300" and width ="300"';
$replaced = preg_replace("/ height=[\"']\w+?[\"']/", " ", $string, 1);
print_r($replaced);
// This is the string. My and width="200".The second height="300" and width ="300"