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
Share on other sites

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

Link to comment
Share on other sites

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 |"

Link to comment
Share on other sites

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);

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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-

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.