I have tried php replace but it changes all occurance. I want exact mach only and split | and +
$menu='home|about|product+service+able|contact|ab|';
echo str_replace("ab","book",$menu);
Result:
$result ='home|about|product+service+abel|contact|book|';
Or If example 2
$menu2='home|about|product+service+ab|contact|able|';
Result2
$result ='home|about|product+service+book|contact|able|';
You have to use regular expression to catch 'ab' the the beginning and end of the string https://www.php.net/manual/ro/function.preg-replace.php see code below with some testcases
<?php
$menu='home|about|product+service+able|contact|ab|';
$menu1='home|about|product+service+able|contact|ab';
$menu2='ab|about|product+service+able|contact|abx|';
$menu3='home|ab|product+service+able|contact|abx';
$menu4='home|about|product+service+ab|contact|able|';
$pattern = array('/^ab([|+])/', '/([|+])ab([|+])/', '/([|+])ab$/');
$replacement = array('book${1}', '${1}book${2}', '${1}book');
echo preg_replace($pattern, $replacement, $menu);
echo "\n";
echo preg_replace($pattern, $replacement, $menu1);
echo "\n";
echo preg_replace($pattern, $replacement, $menu2);
echo "\n";
echo preg_replace($pattern, $replacement, $menu3);
echo "\n";
echo preg_replace($pattern, $replacement, $menu4);
echo "\n";
?>
output
home|about|product+service+able|contact|book|
home|about|product+service+able|contact|book
book|about|product+service+able|contact|abx|
home|book|product+service+able|contact|abx
home|about|product+service+book|contact|able|
Since no one offered a non-regex solution, here is mine:
<?php
// Some Configuration
$searchFor = 'ab';
$replaceWith = 'book';
// Sample data
$menu = 'home|about|product+service+able|contact|ab|';
// Actual code
$array = explode('|', $menu);
$resultArray = [];
foreach ($array as $string) {
if ($string === $searchFor) {
$resultArray[] = $replaceWith;
} else {
$resultArray[] = $string;
}
}
$result = implode('|', $resultArray);
echo $result;
// home|about|product+service+able|contact|book
You could make some optimizations, but for easier understanding i left it as it is.
Update:
If you also want to be able to split between|+
too use the following.
<?php
function customSplit($var, $searchFor, $replaceWith) {
$array = explode('|', $var);
$resultArray = [];
foreach ($array as $string) {
$tmpResult = [];
foreach (explode('|+', $string) as $tmpString) {
if ($tmpString === $searchFor) {
$tmpResult[] = $replaceWith;
} else {
$tmpResult[] = $tmpString;
}
}
$resultArray[] = implode('|+', $tmpResult);
}
return implode('|', $resultArray);
}
// Some Configuration
$searchFor = 'ab';
$replaceWith = 'book';
// Sample data
$menu = 'home|about|product|+service|+able|contact|ab|';
$menu2 = 'home|about|product|+service|+ab|contact|able|';
echo customSplit($menu, $searchFor, $replaceWith);
// home|about|product|+service|+able|contact|book|
echo customSplit($menu2, $searchFor, $replaceWith);
// home|about|product|+service|+book|contact|able|
Look for anyab
not preceded or followed by a character in the rangea-z
.
So you need a negative lookbehind for a-z :(?<![a-z])
and a negative look ahead for a-z :(?![a-z])
withab
in the middle
regex:(?<![a-z])ab(?![a-z])
And in php:
$pattern = '/(?<![a-z])ab(?![a-z])/';
$result = preg_replace($pattern,'book',$yourstring);
function make more easier
function replace_match($find, $replace, $menu)
{
$pattern = array('/^'.$find.'([|+])/', '/([|+])'.$find.'([|+])/', '/([|+])'.$find.'$/');
$replacement = array($replace.'${1}', '${1}'.$replace.'${2}', '${1}'.$replace);
return preg_replace($pattern, $replacement, $menu);
}
echo replace_match('ab','book',$menu);
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.