| |

VerySource

 Forgot password?
 Register
Search
View: 3580|Reply: 11

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

[Copy link]

3

Threads

11

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 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

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 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.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-7-3 18:00:02
| Show all posts
$str = explode("specific character", "string 1 specific string 2");
print_r($str);
Reply

Use magic Report

0

Threads

12

Posts

12.00

Credits

Newbie

Rank: 1

Credits
12.00

 China

Post time: 2020-7-3 23:15:01
| 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.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-7-4 15:45:01
| Show all posts
right
Reply

Use magic Report

3

Threads

11

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-7-5 15:00:02
| 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

17

Posts

17.00

Credits

Newbie

Rank: 1

Credits
17.00

 Invalid IP Address

Post time: 2020-7-7 22:00:02
| 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

27

Posts

18.00

Credits

Newbie

Rank: 1

Credits
18.00

 China

Post time: 2020-7-8 14:00:01
| Show all posts
explode
Reply

Use magic Report

0

Threads

12

Posts

12.00

Credits

Newbie

Rank: 1

Credits
12.00

 Unknown

Post time: 2020-7-9 15:45:01
| Show all posts
$pieces = explode(" ", $pizza, 2);
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-7-13 14:45:01
| Show all posts
strtok() or strpos();
Reply

Use magic Report

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

Points Rules

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

Quick Reply To Top Return to the list