Web Hosting Talk







View Full Version : (php) coder?


Lazarus
06-24-2002, 03:45 PM
Hi,
I'm looking for a php (or other language) coder for a relatively easy job (in my unexperienced opinion).
I have a text file (text.txt) which looks like this:
_root.text=text goes here&end=1
What I need is a php script that makes the "text goes here" appear, but not the "_root.text" or the "&end=1".
I'm assuming php is the best way to go, but maybe I'm wrong... any alternative code is fine as long as it works on a regular server (ASP is NOT supported though).
I'd of course be glad if anyone could provide help on this, but if this turns out to be harder than I thought, then I'd pay someone to do the job.
Please contact me through email with free help :) or quote.
marc.gasserNO-SPAM@urbanet.ch (remove the "NO-SPAM")
Thank you.
-laz.

The Laughing Cow
06-24-2002, 07:42 PM
If this is what I think you are saying it should be simple, I can't think of the PHP code off hand but you want to just snip the first 11 characters from the start and the last 6 from the end?

Jim_UK
06-24-2002, 09:08 PM
Right here goes.... (apologies if it needs a bit of tweaking, its pretty late and I've no time to test it :) )...


<?php

// read the textfile into a variable called $content

$filename="text.txt";
$fp = fopen($filename, "r") or die("Error opening file");
$content = fread($fp, filesize($filename));

// grab the bit in between the first = and $

preg_match("/=([\w\s]+)&/", $content, $bit_we_want);

// then strip the = and & from each end and put it in $bit

$bit = ereg_replace("(=|&)", "", $bit_we_want[0]);

// do whatever you want with $bit here eg print it to screen

echo $bit;

?>


Hope this helps :)

#fdd700
06-24-2002, 09:49 PM
Lazarus
If you've got only one line in your text.txt and text to extract doesn't have any "=" and "&" inside, you might get away with this:
<?
$fp = fopen("text.txt", "r") or die("Error opening file");
$content = fread($fp, filesize("text.txt"));
fclose($fp);
$temp=explode("&",$content);
$temp=explode("=",$temp[0]);
echo $temp[0];
?>
Your extracted text is in $temp[0].

If you've got more than one line, theis code can be put in a loop.
If it's always "_root.text=" and "&end=1", it's even easier:

<?
$fp = fopen("text.txt", "r") or die("Error opening file");
$content = fread($fp, filesize("text.txt"));
fclose($fp);
$content=str_replace("_root.text=","",$content);
$content=str_replace("&end=1","",$content);
echo $content;
?>
Your extracted text is in $content.

Lazarus
06-25-2002, 03:01 AM
Thanks a lot to both of you, your codes were very helpful.
I used parts of it and parts of another one I got on another board, and finnaly got it to work. Not the most elegant way, I'm sure, but here's the code for anyone who's interested.

<?php

// read the textfile into a variable called $content

$filename="thecstext.txt";
$fp = fopen($filename, "r") or die("Error opening file");
$filecontent = fread($fp, filesize($filename));

$string = ereg_replace( "_root.text=(.*)&", "\\1", $filecontent );
$string = ereg_replace("&end=1","","$string");
$string = ereg_replace("end=1","","$string");

echo $string;

?>

Thanks again.
-Laz.
loves WHT