0

I have this string (98)

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

I need to break it at string position 57.

My intention is find the first occurrence of a space character (regex \s) going backward from pos 57.

Then break the string there, so the string would split like this:

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

I have tried multiple ways and can't seem to solve it:

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

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

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

Any help would be extremely appreciated.

4
  • Unfortunately maybe the string did not break at interior but that is besides the point. The point is that a word could be broken at 57, in that case I need the string split at the proper location which would be at the whitespace prior to str pos 57. Commented Mar 2, 2015 at 9:16
  • Then that position would always be this strrpos($string, ' ', 57); Commented Mar 2, 2015 at 9:17
  • No it wouldn't this algorithm will be implemented in a dynamic program. Commented Mar 2, 2015 at 9:18
  • I need the string split at the proper location which would be at the whitespace prior to str pos 57. If that is the exact description then yes it would be. Regardless of how long the string is. Commented Mar 2, 2015 at 9:19

4 Answers 4

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

Try this simple regex.See demo.This will allow regex to capture untill 57 and then backtrack untill it finds a space.The lookahead will make it backtrack untill it finds space.

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

Sign up to request clarification or add additional context in comments.

2 Comments

Hmm this seems like a promising approach. I will work with this and see how it goes with the other strings loaded dynamically.
It looks like it will, I just have to make sure before I accept the answer. But so far this looks like the answer. I will implement it in a sec and come back.
0

Just use a positive lookahead and split on that.

\s(?=Interior)

The above matches only the \s which is followed by Interior

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

DEMO

1 Comment

have any reason for downvoting to let the answer improve or know what's wrong or just mindless downvoting?
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
)
*/

Comments

0

This one will do:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.