Jump to content

Insert character before words...


WeirdMystery

Recommended Posts

Hi, I am trying to insert "| " before words, so I can use the PHP explode function on them.

 

Example: "1 weirdmystery 36668 89 2 hdlowrider 1042 11 3 ds_blockcommunity 301 5 4 xredx22 141 4 5 nitt_from_bcy 94 2 6 noobwithagun 47 1" would turn into this "1 | weirdmystery 36668 89 2 | hdlowrider 1042 11 3 | ds_blockcommunity 301 5 4 | xredx22 141 4 5 | nitt_from_bcy 94 2 6 | noobwithagun 47 1".

 

I tried str_replace but that doesn't have a position parameter so I don't replace the word or letter itself.

 

Must be something with preg_replace and some regrex I don't know of.

 

Thanks...

Link to comment
https://forums.phpfreaks.com/topic/200406-insert-character-before-words/
Share on other sites

  Quote

Does not.

 

Sat May  1 19:28:19 EDT 2010
[7 files, 3.4MB] ~/Desktop
[eureka@dc Desktop]$ php test.php
1 | weirdmystery 36668 89 2 | hdlowrider 1042 11 3 | ds_blockcommunity 301 5 4 | xredx22 141 4 5 | nitt_from_bcy 94 2 6 | noobwithagun 47 1

 

That regrex returns "1 | weirdmystery 37179 | 90 | 2 | hdlowrider 1042 | 11 | 3 | ds_blockcommunity 301 | 5 | 4 | xredx22 | 141 | 4 | 5 | nitt_from_bcy 94 | 2 | 6 | noobwithagun 47 | 1 |"

  Quote

o.O

 

Post the code you used.

 

$content_lower = strtolower($parsed);
$strip_tags = strip_tags($content_lower);
$remove_tabs = str_replace("	", "", $strip_tags);
$remove_linebreaks = str_replace(array("\r", "\r\n", "\n"), "", $remove_tabs);
$remove_before_first_number = preg_replace('#^([^\d]+)?#','', $remove_linebreaks);
//Your solution...
$final = preg_replace('#(\d)\s([^\d])#', '$1 | $2', $remove_before_first_number);

 

  Quote

  Quote

o.O

 

Post the code you used.

 

$content_lower = strtolower($parsed);
$strip_tags = strip_tags($content_lower);
$remove_tabs = str_replace("	", "", $strip_tags);
$remove_linebreaks = str_replace(array("\r", "\r\n", "\n"), "", $remove_tabs);
$remove_before_first_number = preg_replace('#^([^\d]+)?#','', $remove_linebreaks);
//Your solution...
$final = preg_replace('#(\d)\s([^\d])#', '$1 | $2', $remove_before_first_number);

 

Sorry, must have been the Forum's formatting which removed the extra spaces...

Now thats the string that I was trying to modify.

1  weirdmystery   37179   90   2  hdlowrider   1042   11   3  ds_blockcommunity   301   5   4  xredx22   141   4   5  nitt_from_bcy   94   2   6  noobwithagun   47   1

 

  Quote

And you couldn't figure out how to change my regex to accommodate that?

 

$final = preg_replace('#(\d)\s+([^\s\d])#', '$1 | $2', $remove_before_first_number);

 

Sorry, I have no knowledge about regrexes...

 

Just to confirm that, I did not know regrex was spelled regex.

 

Oops, forgot to say thanks!...

You could just create the array using preg_match_all:

 

<?php

$str = "1  weirdmystery   37179   90   2  hdlowrider   1042   11   3  ds_blockcommunity   301   5   4  xredx22   141   4   5  nitt_from_bcy   94   2   6  noobwithagun   47   1";

// No need to explode, use preg match to create the array you need.
$final = preg_match_all('/(\d+)\s+([a-z0-9_]+)\s+(\d+)\s+(\d+)/i', $str, $matches);

// Then you can loop the results like so;
for($i=0;$i<count($matches[0]);$i++){
echo('
	Player ID: '.$matches[1][$i].'		<br />
	Player Name: '.$matches[2][$i].'		<br />
	Player Score?: '.$matches[3][$i].'		<br />
	Player Team Points?: '.$matches[4][$i].'		<br />		<br />
');
}

?>

 

-cb-

  Quote

You could just create the array using preg_match_all:

 

<?php

$str = "1  weirdmystery   37179   90   2  hdlowrider   1042   11   3  ds_blockcommunity   301   5   4  xredx22   141   4   5  nitt_from_bcy   94   2   6  noobwithagun   47   1";

// No need to explode, use preg match to create the array you need.
$final = preg_match_all('/(\d+)\s+([a-z0-9_]+)\s+(\d+)\s+(\d+)/i', $str, $matches);

// Then you can loop the results like so;
for($i=0;$i<count($matches[0]);$i++){
echo('
	Player ID: '.$matches[1][$i].'		<br />
	Player Name: '.$matches[2][$i].'		<br />
	Player Score?: '.$matches[3][$i].'		<br />
	Player Team Points?: '.$matches[4][$i].'		<br />		<br />
');
}

?>

 

-cb-

 

Thanks man, thats even a better solution...

 

Sorry guys for not telling you guys that the number before the username is the user id.

 

(BTW, its a Folding@home statistics lister for our team.)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.