| |

VerySource

 Forgot password?
 Register
Search
View: 32|Reply: 8

How to divide a string into two with a specific character as the boundary?

[Copy link]

1

Threads

2

Posts

3

Credits

Newbie

Rank: 1

Credits
3

 China

Post time: 2020-3-23 15:30:01
| Show all posts |Read mode
As the title.
Divide a string into two with only the first specific character and read it out.
Reply

Use magic Report

0

Threads

2

Posts

3

Credits

Newbie

Rank: 1

Credits
3

 Invalid IP Address

Post time: 2020-7-2 22:45:01
| Show all posts
what do you mean?
Reply

Use magic Report

0

Threads

1

Posts

2

Credits

Newbie

Rank: 1

Credits
2

 China

Post time: 7 day(s) ago
| Show all posts
$str = explode("specific character", "string 1 specific string 2");
print_r($str);
Reply

Use magic Report

0

Threads

12

Posts

12

Credits

Newbie

Rank: 1

Credits
12

 China

Post time: 7 day(s) ago
| Show all posts
explode - Split a string with another string
Example 1. explode() example

<?php
// Example 1
$pizza = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2

// Example 2
$data = "foo:*:1023:1000::/home/foo:/bin/sh";
list($user, $pass, $uid, $gid, $gecos, $home, $shell) = explode(":", $data);
echo $user; // foo
echo $pass; // *

?>



Example 2. Example limit parameter

<?php
$str ='one|two|three|four';

// positive limit
print_r(explode('|', $str, 2));

// negative limit (since PHP 5.1)
print_r(explode('|', $str, -1));
?>

The above example will output:

Array
(
    [0] => one
    [1] => two|three|four
)
Array
(
    [0] => one
    [1] => two
    [2] => three
)




Note: This function can be safely used for binary objects.
Reply

Use magic Report

0

Threads

1

Posts

2

Credits

Newbie

Rank: 1

Credits
2

 China

Post time: 6 day(s) ago
| Show all posts
right
Reply

Use magic Report

1

Threads

2

Posts

3

Credits

Newbie

Rank: 1

Credits
3

 China

 Author| Post time: 5 day(s) ago
| Show all posts
Right

I mean, for example:
$pizza = "piece1 piece2 piece3 piece4 piece5 piece6";

Taking "" space as a character, the space is divided into two starting from a space, and the first part is of course "piece1". The latter part is "piece2 piece3 piece4 piece5 piece6".

How to divide this way?
Reply

Use magic Report

0

Threads

13

Posts

12

Credits

Newbie

Rank: 1

Credits
12

 Invalid IP Address

Post time: 3 day(s) ago
| Show all posts
The landlord wants this interception?

It can be said...

$stra = explode("specific character",$str);
$str1=$stra[0];
for ($i=1;$i<count($stra);$i++)
{
  $str2=$str2."Specific Character".$stra[$i];
}
print_r($str1);
print_r($str2);

Of course, if you want to extract the second one, use the above method, deformed, it is OK.

Although the above is the method, but it is not the real solution ~ use regular. I am new to regular learning, so I can’t give you a regular way
Reply

Use magic Report

0

Threads

26

Posts

15

Credits

Newbie

Rank: 1

Credits
15

 China

Post time: The day before yesterday 14:00
| Show all posts
explode
Reply

Use magic Report

0

Threads

12

Posts

12

Credits

Newbie

Rank: 1

Credits
12

 Unknown

Post time: Yesterday 15:45
| Show all posts
$pieces = explode(" ", $pizza, 2);
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2020|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list