?
Solved

php, text before the pattern

Posted on 2013-02-04
2
Medium Priority
?
240 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 35

Accepted Solution

by:
Terry Woods earned 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

764 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