Preg_replace在以下字符串中

时间:2017-09-12 06:29:19

标签: php regex preg-replace preg-match

我想替换下面的字符串

中的内容

我想将行java.lang.IllegalArgumentException: at android.support.v4.app.FragmentManagerImpl.moveToState (FragmentManager.java:1102) at android.support.v4.app.FragmentManagerImpl.moveToState (FragmentManager.java:1290) at android.support.v4.app.BackStackRecord.popFromBackStack (BackStackRecord.java:1038) at android.support.v4.app.FragmentManagerImpl.popBackStackState (FragmentManager.java:1737) at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate (FragmentManager.java:605) at android.support.v4.app.FragmentActivity.onBackPressed (FragmentActivity.java:179) at android.app.Activity.onKeyUp (Activity.java:2755) at android.view.KeyEvent.dispatch (KeyEvent.java:2712) at android.app.Activity.dispatchKeyEvent (Activity.java:3056) at android.support.v7.app.AppCompatActivity.dispatchKeyEvent (AppCompatActivity.java:543) at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent (WindowCallbackWrapper.java:53) at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.dispatchKeyEvent (AppCompatDelegateImplBase.java:315) at com.android.internal.policy.DecorView.dispatchKeyEvent (DecorView.java:319) at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent (ViewRootImpl.java:4378) at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess (ViewRootImpl.java:4349) at android.view.ViewRootImpl$InputStage.deliver (ViewRootImpl.java:3893) at android.view.ViewRootImpl$InputStage.onDeliverToNext (ViewRootImpl.java:3946) at android.view.ViewRootImpl$InputStage.forward (ViewRootImpl.java:3912) at android.view.ViewRootImpl$AsyncInputStage.forward (ViewRootImpl.java:4039) at android.view.ViewRootImpl$InputStage.apply (ViewRootImpl.java:3920) at android.view.ViewRootImpl$AsyncInputStage.apply (ViewRootImpl.java:4096) at android.view.ViewRootImpl$InputStage.deliver (ViewRootImpl.java:3893) at android.view.ViewRootImpl$InputStage.onDeliverToNext (ViewRootImpl.java:3946) at android.view.ViewRootImpl$InputStage.forward (ViewRootImpl.java:3912) at android.view.ViewRootImpl$InputStage.apply (ViewRootImpl.java:3920) at android.view.ViewRootImpl$InputStage.deliver (ViewRootImpl.java:3893) at android.view.ViewRootImpl$InputStage.onDeliverToNext (ViewRootImpl.java:3946) at android.view.ViewRootImpl$InputStage.forward (ViewRootImpl.java:3912) at android.view.ViewRootImpl$AsyncInputStage.forward (ViewRootImpl.java:4072) at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent (ViewRootImpl.java:4233) at android.view.inputmethod.InputMethodManager$PendingEvent.run (InputMethodManager.java:2430) at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback (InputMethodManager.java:1994) at android.view.inputmethod.InputMethodManager.finishedInputEvent (InputMethodManager.java:1985) at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished (InputMethodManager.java:2407) at android.view.InputEventSender.dispatchInputEventFinished (InputEventSender.java:141) at android.os.MessageQueue.nativePollOnce (Native Method) at android.os.MessageQueue.next (MessageQueue.java:323) at android.os.Looper.loop (Looper.java:136) at android.app.ActivityThread.main (ActivityThread.java:6290) at java.lang.reflect.Method.invoke (Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:776) 替换为结束p标记。

The part has been repaired to

我在preg_replace函数上面尝试了但是它没有按预期工作。

2 个答案:

答案 0 :(得分:1)

preg_replace函数在应用模式后返回字符串的值。将结果分配给变量并将print_r变量分配。

$text = "<p>701082 Range Control Board from Dacor is a manufacturer approved part. The part has been repaired to Dacor's specifications resulting in the highest performance with superior quality</p>";

$result  = preg_replace('/The part has been repaired to.*?<\/p>/U', '</p>', $text);

print_r($result);

答案 1 :(得分:0)

您可以使用strpos查找&#34; end&#34;的位置。并使用substr对其进行子串。

$text = "<p>701082 Range Control Board from Dacor is a manufacturer approved part. The part has been repaired to Dacor's specifications resulting in the highest performance with superior quality</p>";

Echo substr($text, 0, strpos($text, "The part has been repaired to")). "</p>";

https://3v4l.org/tR5tt