/    Sign up×
Community /Pin to ProfileBookmark

How To Join Words ?

Folks,

Can you show me how you join words in a string ?
Say my $string looks like this:
“Every Day, every where and in every way”.
Now let’s join the “Every Day” to “EveryDay”.
And join the “every where” to “everywhere”.
And “every way” to “everyway”.
I just need to learn how to combine words based on my choosing. Here my choosing was “every”.
No. Don’t do it using str_replace() replacing the space after the “every”. There is another way to do it which I need to learn.
Maybe implode() or explode() or substring() or subpos() or whatever you use, show me how you do it.

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@developer_webauthorMay 21.2021 — Folks,

Remember that, we are not dealing with arrays here and so implode()/explode() is irrelevant here.
Copy linkTweet thisAlerts:
@developer_webauthorMay 21.2021 — @sempervivum

Have you tried anything like this before ? You must have! It's basic playing around.
Copy linkTweet thisAlerts:
@SempervivumMay 21.2021 — @developer_web#1631928 No, I never tried this before, but this works for me:
``<i>
</i>$teststr = 'Every Day, every where and in every way';
if (preg_match_all('/[eE]verys+w+/', $teststr, $matches) !== false) {
var_dump($matches);
}<i>
</i>
``
Copy linkTweet thisAlerts:
@developer_webauthorMay 21.2021 — @Sempervivum#1631938

Thanks a bunch!

But can you try one without regex ?
Copy linkTweet thisAlerts:
@SempervivumMay 21.2021 — I gave it a try and used explode but faced the limitations at once. Try this:
``<i>
</i>$splitted = explode(' ', $teststr);
for ($i = 0; $i &lt; count($splitted) - 1; $i++) {
$part = $splitted[$i];
if ($part == 'Every' || $part == 'every') {
echo $part . ' ' . $splitted[$i + 1] . '&lt;br&gt;';
}
}<i>
</i>
``

The comma is remaining. Regex truely is the best bet when handling strings.
Copy linkTweet thisAlerts:
@developer_webauthorMay 22.2021 — @Sempervivum#1631951

Excellent! This seems simple enough to learn!

Thanks a bunch!
Copy linkTweet thisAlerts:
@NogDogMay 22.2021 — Regex is the only way I'd consider doing this; but just for fun (and to over-complicate things):
[code=php]
$string = 'Every Day, every where and in every way';
$result = '';
$parts = explode(' ', $string);
foreach($parts as $ix => $text) {
if($ix == 0 or strtolower($parts[$ix - 1]) == 'every') {
$result .= $text;
} else {
$result .= " $text";
}
}
echo $result;
// EveryDay, everywhere and in everyway
[/code]
Copy linkTweet thisAlerts:
@developer_webauthorMay 23.2021 — @NogDog#1631992

Thanks. This looks good to learn too!
×

Success!

Help @developer_web 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 3.28,
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: @darkwebsites540,
tipped: article
amount: 10 SATS,

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

tipper: Anonymous,
tipped: article
amount: 10 SATS,
)...