Anton Gildebrand
Anton Gildebrand

Reputation: 3707

PHP string replace firstname with first letter in first name

I have i string in my PHP-code which looks like this

Bokad av Anna Göransson 14:45 - 15:45

Now i want to replace "Anna" with "A.". How can i do that using regexp and PHP?

Upvotes: 0

Views: 192

Answers (2)

user557846
user557846

Reputation:

You can do use something like this

$foo=str_replace('Anna', 'A',$foo);

Upvotes: 0

Tarik
Tarik

Reputation: 81731

You can use something different if the string is dynamic and subject to change.

$string = "okad av Anna Göransson 14:45 - 15:45";
$names = explode(" ",$string);
$names[2] = strtoupper($names[2][0]).".";
$string = implode(" ",$names);

Upvotes: 2

Related Questions