困难的PHP字符串函数

时间:2015-03-02 08:59:55

标签: php regex string

我有这个字符串(98)

1994 Acura Integra Type R OEM B18C5 OEM Tranny All Stock Interior. 165,000 MI $9500 (000) 000-1696 

我需要在字符串位置57处将其分解。

我的目的是找到第一个出现的空格字符(正则表达式\s)从位置57向后移动。

然后在那里打破字符串,所以字符串会像这样拆分:

1994 Acura Integra Type R OEM B18C5 OEM Tranny All Stock // str1
Interior. 165,000 MI $9500 (000) 000-1696                // str2

我尝试了多种方法,似乎无法解决它:

$charAt58 = substr($content, 57, 1);

$hasWhiteSpaceAt58 = preg_match('/\s/', $charAt58);

if (!$hasWhiteSpaceAt58) {
    echo "false"; 
    $contentr = strrev($content);
    echo "<br>";
    echo strpos($contentr, " ", 57);
}

非常感谢任何帮助。

4 个答案:

答案 0 :(得分:2)

^.{1,57}(?=\s)

尝试使用这个简单的正则表达式。请参阅演示。这将允许正则表达式捕获直至57然后backtrack,直到它找到space。前瞻将使其回溯直到它找到空间

https://regex101.com/r/wU7sQ0/37

答案 1 :(得分:0)

只需使用积极的前瞻并分开即可。

\s(?=Interior)

上述内容仅与\s匹配,后面跟Interior

var_dump(preg_split("/\s(?=Interior)/", $input_line));

DEMO

答案 2 :(得分:0)

这个会做:

$pos = strrpos($content, ' ', 57);
$output = substr($content, 0, $pos) .'\n'. substr($content, $pos); 

答案 3 :(得分:0)

$s = '1994 Acura Integra Type R OEM B18C5 OEM Tranny All Stock Interior. 165,000 MI $9500 (000) 000-1696';
$parts = str_split($s, strrpos(substr($s, 0, 57), ' ')+1);
print_r($parts);

/*
Array
(
    [0] => 1994 Acura Integra Type R OEM B18C5 OEM Tranny All Stock
    [1] => Interior. 165,000 MI $9500 (000) 000-1696
)
*/
相关问题