localroot
localroot

Reputation: 556

PHP - Regex Remove anything that is not alphanumeric by keeping some exception

I want to remove anything that is not alphanumeric regardless of lowercase or uppercase and replace with ' '. But some exceptions are there.

exceptions are

'.!?

But allowing single quote is a headache and I already searched a lot in Stack-overflow didn't find any answer for my requirement.

$text = preg_replace( '/[^\da-z !\' ?.]/i', ' ', $text );

I tried the above regex but it's replacing single quotes also. But i need to keep that and replace all other non alpha-numeral characters with empty space. Can somebody help me with this?

For eg:

$string_input = "So one of the secrets of producing link-worthy! * content is to write quality content that’s share-worthy!"

$string_output = "So one of the secrets of producing link worthy! content is to write quality content that’s share-worthy!"

Upvotes: 0

Views: 251

Answers (1)

Martin Winkel
Martin Winkel

Reputation: 101

You can use the NOT-pattern in regex:

<?php 
echo implode(' ', preg_split('#[^a-z0-9\.\?\'!]#i', $input));

You cannot use preg_replace in a simple way to replace all at once. But you can explode on the regex and implode them with a space.

Explaining the regex:

# are delimiter

[] Makes a group

^ all within the group are NOT matched (inverter)

a-z Do not match characters a to z

0-9 Match character 0 to 9

Other characters are escaped.

i flag to make match case insensitive.

Upvotes: 1

Related Questions