Solved

PHP/REGEX: everything before certain characters

Posted on 2011-03-09
2
568 Views
Last Modified: 2012-06-22
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
Comment
Question by:hankknight
2 Comments
 
LVL 8

Accepted Solution

by:
ragnarok89 earned 500 total points
Comment Utility
your regex expression will be [0-9:!(].*$

this matches 0-9, :, !, and ( and any thing after until the end of the line
0
 
LVL 16

Author Comment

by:hankknight
Comment Utility
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 run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now