/    Sign up×
Community /Pin to ProfileBookmark

Adding space after number before letter

Hi,
What i want to do is search through a string and when a number follwed by a letter (either case) is found a space character is to be inserted.

For example:-

[code=php]

$my_str=”dtd 3C lalla 3.00pm 12 oo ahh this is 100as 14r 3″;

//This would become:

$new_str=”dtd 3 C lalla 3.00 pm 12 oo ahh this is 100 as 14 r 13″;

[/code]

I know that the use of [0-9] and [a-z] will be required but i do not know how to implement it.

Cheers

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@Tony_shuJul 10.2004 — This will do exactly what you wanted ?

Its tested and proven ? ... sorry its big & messy ... but its the best I could do. Enjoy

[code=php]<?php

$my_str = "abdsdfs 3432adf2 asdF32 dfs2df";

echo "$my_str: $my_str<br>n";

while ( preg_match("/([0-9]{1}[a-z]{1}|[a-z]{1}[0-9]{1})/i", $my_str, $matches) ) {
foreach ($matches as $match) {
$split = preg_split("//", $match);
array_shift($split);
array_pop($split);
$joined = implode(" ", $split);
$my_str = preg_replace("/$match/", "$joined", $my_str);
echo "new $my_str: $my_str<br>n";
}
}

$new_str = $my_str;

echo "$new_str: $new_str";

?>[/code]
...You can test it on its own if you like...which is why I left in all the echo's
Copy linkTweet thisAlerts:
@NaemoauthorJul 10.2004 — Thank you ?
Copy linkTweet thisAlerts:
@pyroJul 10.2004 — Here's an easier way... ?

[code=php]<?PHP
$my_str = "dtd 3C lalla 3.00pm 12 oo ahh this is 100as 14r 3";
$new_str = preg_replace("/(\d)([a-z])/i", "$1 $2", $my_str);
echo $new_str;
?>[/code]
Copy linkTweet thisAlerts:
@Tony_shuJul 11.2004 — [i]Originally posted by pyro [/i]

[B]Here's an easier way... ?



[code=php]<?PHP
$my_str = "dtd 3C lalla 3.00pm 12 oo ahh this is 100as 14r 3";
$new_str = preg_replace("/(\d)([a-z])/i", "$1 $2", $my_str);
echo $new_str;
?>[/code]
[/B][/QUOTE]

...was that all i needed to do? add the parenthesis?! lol

I tried to deal with d & use $1 & $2 but they returned nothing. Humph
Copy linkTweet thisAlerts:
@pyroJul 11.2004 — Yes, the parenthesis tell the regular expression to remember the match. ?
Copy linkTweet thisAlerts:
@NaemoauthorJul 11.2004 — Hi

Thanks for all you responses they have been really helpful but one last thing.

I now want to search the string for a number and just a number. The number could be one or two digits. When the number is found a newline is inserted after the number. Example:-

[code=php]
$mstr="This is some 3 text here 3.00pm and some 12 more!"

//Would become

"This is some 3
text here 3.00pm and some 12
more"

[/code]


I have some code that i dared to write as regular expressions go way over my head, but it didnt work ?

[code=php]

$text_string=str_replace("(s[0-9]s)/i","n",$my_string);

[/code]


Yes... well any help would be appreciated.

Thank-you!

EDIT: I just realised i'm using str_replace and your using preg_replace, whats the difference?
Copy linkTweet thisAlerts:
@Tony_shuJul 12.2004 — [code=php]$text_string = preg_replace("/s(d{1,2}s)/i", "$1n", $my_string);[/code] ...This does what you asked...you forgot the "" with ur "s"s ... & you put a "" instead of "/" in the beginning.

From my understanding ... preg_replace() is the same as str_replace, except it "handles" similarly to PERL's (s///) function, because you can use regex characters. Using regex characters (such as s, ^, $, etc ... allows you to search for patterns, where as in str_repace()...I believe it only searches for "matches" of your "subject".
×

Success!

Help @Naemo spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...