Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String manipulation 2

Status
Not open for further replies.

spamjim

Instructor
Mar 17, 2008
1,368
US
I've got two issues while playing with a string.

1) What would be the best way to divide a string using every other character?

$source = "12345678ABCDEFGH";

// intended output:
// $outputone = "1357ACEG";
// $outputtwo = "2468BDFH";

2) What would be the best way to divide a string into segments of a particular length?

$source = "1357ACEG";

// intended output for a length of 2:
// $output[0] = "13";
// $output[1] = "57";
// $output[2] = "AC";
// $output[3] = "EG";
 
Hi

1)
Code:
$source = "12345678ABCDEFGH";
$outputone = preg_replace("/(.)./","\\1",$source);
$outputtwo = preg_replace("/.(.)/","\\1",$source);
2)
Code:
$source = "1357ACEG";
preg_match_all("/.{2}/",$source,$output);
print_r($output[0]);

Feherke.
 
I get the impression that I really, really need to better learn regular expressions. This makes perfect sense. Thank you!
 
Of course if you don;t feel inclined to use regular expressions, there are other ways this can be done.

Yes it will be a little longer in code, but works just as well, and you avoid having to use regular expressions which even the PHP Online manual suggests only be used when absolutely necessary.

Code:
<?PHP
$source = "12345678ABCDEFGH";
$output1="";
$output2="";

$j=0;
for($i=0;$i<=strlen($source)-1;$i++){
	if($j==0){
	$output1.=$source[$i];
	$j=1;
	continue;
        }

	if($j==1){
	$output2.=$source[$i];
	$j=0;

	}
}

echo $output1 . "and" . $output2;
$sets=array();

for($i=0;$i<=strlen($output1)-1;$i++){
$sets[]=substr($output1,$i,2);
$i++;

}
echo "<pre>";
print_r($sets);
echo "</pre>";

?>


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Hi

Of course, this is matter of habits. ;-)
Code:
[gray]// Phil style[/gray]
for($i=0;$i<=strlen($output1)-1;[red]$i++[/red]){
$sets[]=substr($output1,$i,2);
[red]$i++[/red];

}

[gray]// Feherke style[/gray]
for($i=0;$i<=strlen($output1)-1;[red]$i+=2[/red])
  $sets[]=substr($output1,$i,2);
Anyway, I just want to show a reason to learn regular expressions. Here are some code spinets in other languages which work the same way as my previously posted regular expressions in PHP :
Perl:
$source="12345678ABCDEFGH";
($outputone=$source)=~s/[red](.).[/red]/[green]\1[/green]/[blue]g[/blue];
($outputtwo=$source)=~s/[red].(.)[/red]/[green]\1[/green]/[blue]g[/blue];

$source="1357ACEG";
@output=$source=~m/[red].{2}[/red]/[blue]g[/blue];
Code:
set source "12345678ABCDEFGH"  
set outputone [regsub [blue]-all[/blue] {[red](.).[/red]} $source [green]{\1}[/green]]
set outputtwo [regsub [blue]-all[/blue] {[red].(.)[/red]} $source [green]{\1}[/green]]

set source "1357ACEG"
set output [regexp -inline [blue]-all[/blue] {[red].{2}[/red]} $source]
JavaScript:
source="12345678ABCDEFGH"
outputone=source.replace(/[red](.).[/red]/[blue]g[/blue],'[green]$1[/green]')
outputtwo=source.replace(/[red].(.)[/red]/[blue]g[/blue],'[green]$1[/green]')

source="1357ACEG"
output=source.match(/[red].{2}[/red]/[blue]g[/blue])
Ruby:
source="12345678ABCDEFGH"
outputone=source.[blue]g[/blue]sub /[red](.).[/red]/,"[green]\\1[/green]"
outputtwo=source.[blue]g[/blue]sub /[red].(.)[/red]/,"[green]\\1[/green]"

[gray]// No Ruby solution for question 2) for now. I am still learning.[/gray]
The amount of code to rewrite and the differences between languages are slightly smaller then in case of Phil's code. So I would say, the knowledge related to regular expressions is quite portable. Which makes me (ab)use them regularly.

Feherke.
 
Is regex to be used as a last resort as it uses greater resources? I'm usually asking odd questions here to determine the more efficient use of PHP but in this particular case, the function is rarely used and can use up any resource it wants.

I don't know why I didn't see it earlier but my second issue is solved easily with substr wrapped in a 'while' loop that steps through positions in the string.

Thanks everyone!
 
the issue with regex is that the preg/ereg modules are not loaded at runtime, or even pre-loaded when php/web server is started. they are kicked off when needed and then each regular expression is individually compiled. thus there is a system level overhead in their use.

however php6 changes this in that it preloads the preg* engine which should make use of the regex more attractive. By way of warning, ereg_* will no longer be available.

One word of further warning: i read about the change to preloading PCRE in php6 some while ago and have been repeating the news from time to time. However as I come to search for a source today, in the brief time available I have not been able to find anything. If anyone can point to a source that would be great. If not, then maybe the goal posts have changed and readers should treat my news with pinch or two of salt...
 
Hi

spamjim said:
Is regex to be used as a last resort as it uses greater resources?
By default I ignore the anti-regular expression warnings. I know they are slower and I reduce their use in critical parts of my code.

I used to think to the future : "will I know next month/year what I wrote here now ?".

I used to think to the worst : "will I be able to adjust this to future requirements ?".

The answer of the above two questions usually determine me to choose regular expressions for their clarity and flexibility. Of course, your mileage may vary.


Feherke.
 
to feherke's post, I would add that i have never noticed regex to be a significant overhead on the user experience, at least when compared with the inherent latency of TCP/IP, the internet and browser rendering. and (i've said this a number of times), hardware is so cheap for the everyday site that to add a faster processor or a second box is often significantly cheaper than paying for code optimisation. I know that people take exception to this statement: it's meant to be an observation rather than advice.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top