Solved

Copy part of string

Posted on 2007-11-25
9
214 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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
 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
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 dynamically set the form action using jQuery.

776 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