Reputation: 2126
Is that a way to find regex format something like that?
I may have a string or may be a list of line like
Human have hands/fingers/feet and so on
If space not contain behind slash, I want to add space behind slash Human have hands/ fingers/ feet and so on
But, if space contain behind string, I don't want to add extra space. Only want to add space on the rest of the string without space behind slash.
e.g hands/fingers/ feet
to hands/ fingers/ feet
Upvotes: 2
Views: 671
Reputation: 922
Use explode()
OR try the str_replace()
example below
Please try str_replace()
:
<php
$words = 'hands/fingers/ feet'; /** Your example words **/
$words = str_replace('/', '/ ', $words); /** Replace any slash with a slash AND a space **/
$words = str_replace('/ ', '/ ', $words); /** Replace slash with 2 spaces with slash with 1 space as this is what you need **/
echo $words; /** Output: hands/ fingers/ feet **/
?>
I hope this will bring you into the right direction..
Upvotes: 0
Reputation: 21671
Yes, using a negative look ahead is the best way.
$str = preg_replace('/\/(?!\s)/', '/ ', $str);
You can see it [here][1]
This uses a negative look ahead. So it basically says:
Match the /
only if it's not followed by a space. Also look aheads don't consume any characters in the matching so there is no need to replace the letter if matching without a look ahead.
Special thanks to @ShalithaSuranga for posting the same answer I thought up originally, but before I had a chance to post it and thus forcing me to make a bit extra effort to use a look ahead.
I typically don't use them as they can be much more confusing then regular matching patterns.
That said, either one is probably going to be faster then explode or other more lengthy methods. However, in this case speed may not be an issue, I would say go with the one that makes the most sense to you and is easier for you to Read and understand latter.
Readability is always #1, then functionality, then performance.
If you want a short non-regx answer you can use this too
$str = "hands/fingers/feet";
echo implode("/ ", array_map('trim', explode("/", $str)));
As seen here
For this one, array_map
takes a function callback as the first argument, ( which can be a string ) and applies it or maps it to each item in an array. In this case we want trim
which removes whitespace from both sides of a string. Then we implode it back together. This way an array like this from exploding one/ two
[
'one',
' two'
]
will have the whitespace trimmed off, then we implode it with the '/ '
and all set.
Upvotes: 1
Reputation: 6088
You can do it with this regex:
\/(\s)?
Demo: https://regex101.com/r/c67Mh9/1/
PHP example:
echo preg_replace('@\/(\s)?@', '/ ', "Human have hands/fingers/ feet and so on");
Upvotes: 1
Reputation: 1146
Yeah of course with RegEx for this one
$count = null;
$returnValue = preg_replace('/\\/([^ ])/', '/ $1', 'hands/fingers/ feet', -1, $count);
Explanation
/\/([^ ])/
this regex matched /
without space only and replace with '/ $1'. Here $1 is the character after /
Upvotes: 1
Reputation: 2806
Below code can surely work (Tested Offline):
<?php
$string = "hands/fingers/feet";
$spaceArr = explode("/",$string);
$modify_string = '';
foreach($spaceArr as $val){
$modify_string .= $val."/ ";
}
echo $modify_string;
// Output is hands/ fingers/ feet
?>
You can add any much space in hands / fingers/ feet but output will always like hands/ fingers/ feet
Upvotes: 1