Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Copy part of string

Posted on 2007-11-25
9
Medium Priority
?
246 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 200 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 800 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Hire Technology Freelancers with Gigs

Work with 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 This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…
Suggested Courses

618 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