Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 求截取PHP指定字符串代码

求截取PHP指定字符串代码

Jun 23, 2016 pm 02:00 PM

有下面一段html字符串:

aaaa
bbbb
cccc
dddd
 >
要想输出下面内容:
aaaa
dddd
 >
即删除中间两个
前面的内容及
的简易代码,由于还要输出为html,不用数组方式。


回复讨论(解决方案)

1

$s = '<td align="Center" rowspan="2" width="7%">aaaa<br>bbbb<br>cccc<br>dddd<br></td >';echo preg_replace('/<br>\w+/', '', $s, 2);

Copy after login

1

<td align="Center" rowspan="2" width="7%">aaaa<br>dddd<br></td >

Copy after login

还有点小问题,单独运行没问题,但上面的表达式实际用到我的字符串时,因为第二段br里含有( ),,最后一段br里含有“-”,截取出来有点问题。
$s = 'aaaa
11(22)
cccc
dd-dd
';
截取出来变成了:
$s = 'aaaa(22)
cccc-dd
';

1

$s = '<td align="Center" rowspan="2" width="7%">aaaa<br>11(22)<br>cccc<br>dd-dd<br></td >';echo preg_replace('/<br>[\(\)-\w]+/', '', $s, 2);

Copy after login

补充说明:aaaa、cccc是中文。

补充说明:aaaa、cccc是中文。

1

$s = '<td align="Center" rowspan="2" width="7%">中文<br>11(22)<br>拼音<br>dd-dd<br></td >';echo preg_replace('/<br>[\x{4e00}-\x{9fa5}\w\(\)-]+/u', '', $s,2) ;//UTF-8编码

Copy after login

谢谢。第一段截取出来的还是错位,还是直接贴我原始的字符吧:
计算机编程
1-9(1,2)
王某
3-307

要想输出两种效果:
1. 计算机编程
3-307

2.计算机编程
1-9
3-307
第二段只去掉括号及里面内容

1

1

echo preg_replace('/<br>[^<]+/', '', $s, 2);

Copy after login
Copy after login


2

1

echo preg_replace('/\(.+?\)/', '', $s);

Copy after login
Copy after login

1

1

echo preg_replace('/<br>[^<]+/', '', $s, 2);

Copy after login
Copy after login


2

1

echo preg_replace('/\(.+?\)/', '', $s);

Copy after login
Copy after login


谢谢版主,第一条正确了,第二条的“王某
还在。

1

echo preg_replace('/<br>[\x{4e00}-\x{9fa5}\w\(\)\-,]+/u', '', $s,2) ;//UTF-8编码

Copy after login

你多了,逗号在【】方括号里面加进去啊

1

<?php$str = '<td align="Center" rowspan="2" width="7%">aaaa<br>bbbb<br>cccc<br>dddd<br></td >';$arr = explode('<br>', $str);array_splice($arr, 1, 2);echo implode('<br>', $arr);?>

Copy after login

请jordan102帮忙,第二条 echo preg_replace('/\(.+?\)/', '', $s) 还需要截掉“王某
“的部分。

1

echo preg_replace_callback('/<br>[^<]+/', 'foo', $s, 2); function foo($m){     return strpos($m[0], '(') !== false ? preg_replace('/\(.+?\)/','',$m[0]) : '';           }

Copy after login

谢谢,单独对于一句里面的运行没问题了。但前面还有些语句和空格之类的加上运行就有问题了。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

Build a React App With a Laravel Back End: Part 2, React

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Introduction to the Instagram API

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

Notifications in Laravel

See all articles