Solved

Copy part of string

Posted on 2007-11-25
9
203 Views
Last Modified: 2008-02-01
Please read the comment inside the script

<?php
$Messagecode="
100 Can't read
200 Can write
300 Can't write
400 Can write
500 Can't delete
600 Can Delete
700 Upload okay
800 Bad Username.
";

$Code=100;
//some script here.
//something like,  look inside the $Messagecode, if the $Code is match  on of the line  in the $Messagecode then copy that line. and print it.

if(isset($Code) && $Code =='100'){
//copy part of the string, the result should be Can't read
}
if(isset($Code) && $Code =='200'){
//copy part of the string, the result should be Can write
}

****IF NOT MATCH ANY OF THOSE CODE,
Echo "Code not found";
?>
0
Comment
Question by:Ghostspirit
  • 4
  • 3
  • 2
9 Comments
 
LVL 21

Expert Comment

by:nizsmo
ID: 20346637
May I suggest using an array?

<?php

$messagecode = Array(100 => "Can't Read", 200 => "Can Write", 300 => "Can't Write", 400 => "Can Write", 500 => "Can't Delete", 600 => "Can Delete", 700 => "upload OKAY", 800 => "Bad Username");
 

$Code = 100;

echo $messagecode[$Code];
 

?>

Open in new window

0
 
LVL 21

Assisted Solution

by:nizsmo
nizsmo earned 50 total points
ID: 20346649
To make it more like your example:
<?php

$messagecode = Array(100 => "Can't Read", 200 => "Can Write", 300 => "Can't Write", 400 => "Can Write", 500 => "Can't Delete", 600 => "Can Delete", 700 => "upload OKAY", 800 => "Bad Username");
 

$Code = 100;

if(!array_key_exists($Code,$messagecode))

{

	echo "Code does not exist!";

	exit();

}
 

echo $messagecode[$Code];
 

?>

Open in new window

0
 
LVL 20

Accepted Solution

by:
steelseth12 earned 200 total points
ID: 20346756
<?php
$Messagecode="
100 Can't read
200 Can write
300 Can't write
400 Can write
500 Can't delete
600 Can Delete
700 Upload okay
800 Bad Username.
";

$Code=800;

$Code=preg_quote($Code);

$value ='';

if(preg_match("/($Code).*?\n$/",$Messagecode,$match)) {

      $value = trim(str_replace($match[1],"",$match[0]));
      
}

if(isset($Code) && !empty($value)){

      print $value;      

}else{

      print "Code not found";      

}

?>
0
 

Author Comment

by:Ghostspirit
ID: 20346786
The reason I listed my codes by lines is because my script is reading those values from a file.
Is there any alternative choice?


100 Can't read
200 Can write
300 Can't write
400 Can write
500 Can't delete
600 Can Delete
700 Upload okay
800 Bad Username.

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Ghostspirit
ID: 20346945
Script error
seem like steelseth12 's script is having a big bug

The script only delete the last line
RESCUE ME HERE
0
 
LVL 20

Expert Comment

by:steelseth12
ID: 20346952
Ghostspirit what do you mean DELETE the last line ? Please explain more
0
 

Author Comment

by:Ghostspirit
ID: 20346989
my bet,
I mean detect the last line which is the "800 Bad Username."
If I change the $Code to a different number, the script will not able to dectect.

for example $Code=600;
0
 
LVL 20

Expert Comment

by:steelseth12
ID: 20347017
sorry small mistake ..
change

if(preg_match("/($Code).*?\n$/",$Messagecode,$match)) {

with

if(preg_match("/($Code).*?\n/",$Messagecode,$match)) {
0
 

Author Comment

by:Ghostspirit
ID: 20347024
Thank you very
blink blink A++
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now