Solved

PHP/REGEX: everything before certain characters

Posted on 2011-03-09
2
571 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
ID: 35085001
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
ID: 35085827
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

776 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