Solved

Copy part of string

Posted on 2007-11-25
9
192 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
resizeing PHP image 2 22
html input clean up 3 32
Paging Using PHP 7 32
Datepicker in PHP 9 0
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.  …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

758 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

21 Experts available now in Live!

Get 1:1 Help Now