Seek to intercept the specified PHP string code-php Tutorial

Source: Internet
Author: User
The following html string is provided for intercepting the specified PHP string:
Aaaa
Bbbb
Cccc
Dddd

To output the following content:
Aaaa
Dddd

Delete the two
Previous content and
The simple code is also output as html, and the array method is not required.


Reply to discussion (solution)

$s = 'aaaa
bbbb
cccc
dddd
';echo preg_replace('/
\w+/', '', $s, 2);
aaaa
dddd

There is also a small problem. it is okay to run it separately, but when the above expression actually uses my string, because the second section of the br contains (), and the last section of the br contains "-", it is a problem to extract it.
$ S ='Aaaa
11 (22)
Cccc
Dd-dd
';
The screenshot is changed:
$ S ='Aaaa (22)
Cccc-dd
';

$s = 'aaaa
11(22)
cccc
dd-dd
';echo preg_replace('/
[\(\)-\w]+/', '', $s, 2);

Note: aaaa and cccc are Chinese characters.

Note: aaaa and cccc are Chinese characters.

$ S ='Chinese
11 (22)
Pinyin
Dd-dd
'; Echo preg_replace ('/
[\ X {4e00}-\ x {9fa5} \ w \ (\)-] +/U', '', $ s, 2); // UTF-8 encoding

Thank you. The first part of the screenshot is still misplaced. paste my original characters directly:
Computer programming
1-9 (1, 2)
Wang
3-307

To output two effects:
1.Computer programming
3-307

2.Computer programming
1-9
3-307
The second paragraph only removes the brackets and content.

1

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


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

1

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


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


Thank you, moderator. The first article is correct, and the second article "wang"
Still.

Echo preg_replace ('/
[\ X {4e00}-\ x {9fa5} \ w \ (\) \-,] +/U', '', $ s, 2); // UTF-8 encoding

You have more. add a comma in square brackets.

 aaaa
bbbb
cccc
dddd
';$arr = explode('
', $str);array_splice($arr, 1, 2);echo implode('
', $arr);?>

Please help jordan102. The second echo preg_replace ('/\ (. +? \)/', '', $ S) also needs to cut off" Wang
.

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

Thank you. it is okay to run the statement separately. However, some of the preceding statements and spaces may cause problems.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.