php, text before the pattern

general question
(not looking for a pattern)
(must work with php)

for a
$pattern
and
$text

$new_text = $text before  $pattern
LVL 1
rgb192Asked:
Who is Participating?
 
Terry WoodsConnect With a Mentor IT GuruCommented:
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
 
rgb192Author Commented:
thanks
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.

All Courses

From novice to tech pro — start learning today.