Link to home
Start Free TrialLog in
Avatar of Ghostspirit
Ghostspirit

asked on

Copy part of string

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";
?>
Avatar of Joe Wu
Joe Wu
Flag of Australia image

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

SOLUTION
Avatar of Joe Wu
Joe Wu
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ghostspirit
Ghostspirit

ASKER

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.

Script error
seem like steelseth12 's script is having a big bug

The script only delete the last line
RESCUE ME HERE
Ghostspirit what do you mean DELETE the last line ? Please explain more
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;
sorry small mistake ..
change

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

with

if(preg_match("/($Code).*?\n/",$Messagecode,$match)) {
Thank you very
blink blink A++