Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 578
  • Last Modified:

PHP/REGEX: everything before certain characters

Using PHP and REGEX, I would like only everything before certain characters.

Everything after any of the following characters, including those characters, should be removed.

:
!
0
1
2
3
4
5
6
7
8
9
(


For example, this:
Hello World! This is a test.

Should become this:
Hello World

This:
Hi 123

Should become this:
Hi
0
hankknight
Asked:
hankknight
1 Solution
 
ragnarok89Commented:
your regex expression will be [0-9:!(].*$

this matches 0-9, :, !, and ( and any thing after until the end of the line
0
 
hankknightAuthor Commented:
Thanks!  Here is an example of the working code:
<?php

$data = 'Hello, world 0432! Zooph 123';
echo preg_replace('#[0-9:!(].*$#', '$1', $data );

?>

Open in new window

0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now