[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 251
  • Last Modified:

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";
?>
0
Ghostspirit
Asked:
Ghostspirit
  • 4
  • 3
  • 2
2 Solutions
 
nizsmoDeveloperCommented:
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
 
nizsmoDeveloperCommented:
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
 
steelseth12Commented:
<?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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
GhostspiritAuthor Commented:
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
 
GhostspiritAuthor Commented:
Script error
seem like steelseth12 's script is having a big bug

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

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

with

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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now