Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 581
  • 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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