Solved

php, text before the pattern

Posted on 2013-02-04
2
236 Views
Last Modified: 2013-02-08
general question
(not looking for a pattern)
(must work with php)

for a
$pattern
and
$text

$new_text = $text before  $pattern
0
Comment
Question by:rgb192
2 Comments
 
LVL 35

Accepted Solution

by:
Terry Woods earned 500 total points
ID: 38853296
This will get the text up to the first occurrence of the pattern:

$pattern_without_delimiters = "mypattern.*ends here";
$new_text = preg_replace("#^(.*?){$pattern_without_delimiters}#s", "$1", $text);

Open in new window


Update: removed preg_quote call because it would change .* to \.\*
You'll need to escape any # characters that you want to match in your pattern (with a backslash).
0
 

Author Closing Comment

by:rgb192
ID: 38870532
thanks
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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 dynamically set the form action using jQuery.

828 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