我如何将一个字符串(例如:-$x="hello/welcome" ;)分成 php 中的两个变量

how can i divide one string, (ex:-$x="hello/welcome" ;)to two variable in php

考虑下面的变量 x.is 有什么方法可以在 "slash" 中将其分成两部分并将它们分配给 php 中的两个变量。两个词之间应该只有斜线。

<?php
    $x="hello/welcome";
?>
<?php
$x="hello/welcome";
$a = explode("/", $x);

// $a[0] = "hello"
// $a[1] = "welcome"

?>

有用吗?

您可以同时使用 explode 和 preg_split。试试这个:-

<?php
    $x="hello/welcome";
    $array = explode("/", $x);
    echo "<pre/>";print_r($array);
?>

或者

<?php
    $x="hello/welcome";
    $y = preg_split('^/^',$x);
    print_r($y);
?>

两种情况下的输出都是:-

Array
(
    [0] => hello
    [1] => welcome
)