Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
 
  HOME    TRACKER    PHP




Is There A Php Command To Take A Specific Field From A Delimited Line Of Text?


I have a string which is multiple fields delimited by commas.

I would like a php command that says... take the 3rd field for
example.

ie. If MYVARIABLE has the data "hello,there,big,world".

I would like to be able to say, pickup the 3rd field "big" without
knowing the character positions or lengths.




View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread
How To Echo A Single, Specific Line From A Text File
How do I echo a single, specific line from a text file?  Let's say, for example, the second line?

Exploding Tab Delimited Line
I'm trying to create an array from a tab delimited line of text. I've tried explode('/t',$line); but doesn't seem to work. What am I doing wrong?

CLI ( Command-line ) PHP Displaying Source Code In-line When Programis Executed
I'm using PHP 4.4.2, and use PHP on both the command-line and the web.

I am running PHP on SuSE 10 Linux , in a VMware 5.5 workstation, using
Apache 2.0.55 , on my Dell laptop. Everything has been running flawlessly
without problems. Very amazing to use VMware, it has worked beautifully.

uname -a

Linux xxxxxxx 2.6.13-15.8-default #1 Tue Feb 7 11:07:24 UTC 2006 i686 i686 i386
GNU/Linux

php -v
PHP 4.4.2 (cli) (built: Feb 7 2006 20:13:29)
Copyright (c) 1997-2006 The PHP Group
Zend Engine v1.3.0, Copyright (c) 1998-2004 Zend Technologies

php -m

[PHP Modules]
bcmath
bz2
calendar
ctype
curl
dbx
dio
domxml
exif
ftp
gd
gettext
gmp
iconv
mcrypt
mssql
mysql
ncurses
openssl
overload
pcre
posix
session
shmop
sockets
standard
sysvsem
sysvshm
tokenizer
wddx
xml
yp
zlib

When I run the following program from the command line it prints out the
source code as well as the output from the program. I run other PHP programs
on the command-line and this doesn't happen. It's really peculiar. The
error output is below.

I copied this off of php.net, and have modified it only slightly to allow
command-line input. I was trying to find a CSV solution, which I ended up
finding elsewhere, but am curious why this program would error out the
way it does--have I misconfigured something? My php.ini is basically
unchanged, if at all. I think I might have set the path to mysql but that's
about it. When I run this program from the web it works without error.

<?php

if ( $argv[0] )
{
$file = $argv[0] ;
}
else{
$file = $_GET['file'] ;
print "<font face=arial>
" ;
}

$row = 1;

$handle = fopen("$file", "r");

while ( ( $data = fgetcsv($handle, 1000, ",") ) !== FALSE )
{

$num = count($data);

print "$num fields in line $row:<br>
";

$row++;

for ($c = 0; $c < $num; $c++)
{
print "$data[$c]<br>
" ;

Create A Delimited Text Array
How can I create a delimited text file to work with this script. I would like to put this first part in a text file and load it into an array so the php script can parse it. Code:

Importing To DB From Pipe Delimited Text File
I am trying to make a PHP script that will import data from a pipe delimited text file in to a database. The problem I am having is that the fields have long descriptions in them and there are multiple line breaks for paragraphs. They contain HTML codes as well (<B>).

The multiple line breaks are causing it to import the data strange, putting data in the wrong fields and totally missing some data as well. Here is what the text file Code:

Delete Entire Line In Text File If Anything In The Line Matches Search String
how would I code a php script that would open a text file, look for a string i specified, and if it finds that string, it will delete the entire line in the text file that contained that text. I'm new to php, so sorry if this is really obvious..

Table Display Grouped By Specific Field
I operate a youth sports website and i display my current alignments in a long running list of teams (one field within my table) with association divisions (another field within table).

I would like to group them by divisions, like a two column table for better clarification.

Php Command Line
how would I write a function so that it can accept parameters when running it via command line?

eg.

php -q email-script.php

and somehow pass an email address to that file?

is it possible to have the php file wait for keyboard input?

Command Line
Is it possible to execute PHP from the command line, like PERL? I know PHP, and not PERL, I know they are similar but it would be easier if I didn't have to learn a second language just yet.

How To Use Php On The Command Line?
My question is how to use php on command line in windows

Command Line Or Not???
What's the best way to check whether script is run from command line or by
web server?



PHP Command Line
I am running PHP from the command line, so I can have a huge loop, so... how can get the file to start over once it gets to the end of an array? after the foreach, I was thinking of having:

<?php
header("Location: contentRobot.php");
exit;
?>

But... I'm not sure if that is only valid for browser or not.

PHP Command Line (php.exe)
I am running WAMP Server on my computer, and I need to run a script with the PHP Command Line Interface... I've been doing some research and  so I tried entering the folowing command into the php command window: php -h, that is supposed to show a list of commands... nut it does nothing... nothing I try works... any ideas?

How To Return Just First-line From A Multi-line String Of Text?
I've been looking through the manual for string commands, and I haven't had success in determining the proper call for this, if there is one:

Let's say I have a text string with hard-carriage returns in it:

"The lazy brown fox ran down the road
to the store and bought some eggs
to bake a cake and serve dinner
for all his friends."

So if that string value is in "x", how can I tell it to return JUST the first line: "The lazy brown fox ran down the road"

Just One Line ! (exec Command)
I would most like to just get the first line from exec result. The last line just isn't what I want. When I use passthru it doesn't allow me to store the results in a variable, so that's no good.

Skip To Line Command?
Hello fellow PHPers. I've made a few posts in the past few days and really appreciate your help. Now i'm at the end of my project - getting a database submission/send email form to validate.

I get all the correct errors if the person doesn't enter certain fields, and the data doesn't submit. If they do enter all the required fields, it submits...

However, the code for when it doesn't submit reads...
if ($errmsg!= "") {
echo $errmsg;
} else {
the rest of the code
the rest of the html syntax

My problem is that if the error page comes up, the rest of the HTML doesn't get rendered because the PHP simply quits. Is there a command in PHP in terms of "Go To Line This in the document", so rather than exiting it would just skip the rest of the PHP but still print all the HTML. I thought of switching!= to == but the way in which that works fails as well. I'm thinking I might have to validate straight on the form rather than the submission page, but let me know what you know!

Talking To The Command Line
I have written a c program using Baltimores Encryption solution. I need to be able to access the executable from PHP but have not got the first clue how...
I simply need to pass the executable a string, and expect a string back. The whole function works on the command line, but, AGGHHH, how to i integrate it into PHP...
this is why i love ASP and COM URL.

Command Line Problems
I'm trying to use PHP as a command line executable, but I'm running into a few problems with passing in arguments, mainly, I can't.

I am using PHP version 4.2.3 and I have installed it with --enable-cli. I can run PHP scripts from the command line, no problem. However, no matter what I try, I can't seem to pass in variables and print out their values.

Newbie: How To Run Php From Command Line?
I need to test a script and want to do so on a Linux box (logged in via SSH from
another machine).

I tried creating a test script (called "test.php") like this:

#!/usr/bin/php
<?php
print "test";
?>

and tried running this from the command line like this:

../test.php

but nothing happens.

I also tried this:

../test test.txt

and the text in test.txt is this:

X-Powered-By: PHP/4.2.2
Content-type: text/html

Why is "test" not appearing in the test.txt file?

How do I get "test" to appear on my screen, rather than having to use test.txt?

Do I have to redirect STDOUT? How do I do this?


Command Line Tool
How do I get php to run a command line tool, in Windows Server 2003, and the
continue with the job in hand, is this possible?

Compiling Command-line
I'm trying to install a program (Cacti -- it queries for things like bandwidth usage, etc. every five seconds, and generates graphs from that data); however, it requires that it be able to run "php" from the command-line. (As opposed to through Apache.)

Is there an easy way to recompile PHP with its previous settings, but also configure it to allow me to invoke it from the command line? (If it matters; I compiled it initially with ApacheToolbox, but that's of no help to me for this.)

Running PHP From Command Line
I just installed the latest version of PHP as an Apache Module. I'm wondering, how can I now run PHP from the command line? I heard there was a way without having to reinstall PHP to do this.

Command-line PHP Problem
I am trying to simply pipe in 2 very small commands and not have to
create a PHP script to do this:

Quote:

PHP Only Runs From Command Line
I know very little about PHP, but I was just curious to know
why exactly would some PHP code run fine from the command line but not
from the browser?

I'm just curious. I was working with someone else's PHP code that
supposedly created MySQL tables in a database and for some reason it
just wouldn't create the tables at all (no specific error message).
Someone suggested I run from the command line and when I did, it
worked...

PHP And Command Line (for Wiki)
I started to "program" in PHP (better say to read it :P) from a
few days because I am installing a Wiki on a virtual server (Windows
Server 2003 with Plesk 7.6). I have PHP5 and all is working just fine.

However, I am trying to parse latex equations and encountered some
troubles. I have already installed all the necessary software and when
I execute the following by command line:

C:miktexinlatex.exe --output-directory=C:Inetpubvhostsmysite.com
httpdocswikiimages mp --interaction=nonstopmode C:a.tex

I generate the right file in the right folder. However, when I try to
execute (adding it to a PHP routine that is executed for sure):

$command = "C:miktexinlatex.exe --output-directory=C:Inetpub
vhostsmysite.comhttpdocswikiimages mp --
interaction=nonstopmode C:a.tex";
$text = exec($command);

Running Php From Command Line
I'm trying to run a php script from the command line, but I'm having some difficulties. If I browse to my page in IE, my script executes properly, but if I run it from the command line, I receive this error:

Warning:  fopen() URL file-access is disabled in the server configuration in /www/public_html/update.php on line 21

Warning:  fopen(): failed to open stream: no suitable wrapper could be found in /www/public_html/update.php on line 21

I'm not sure why I receive errors when trying to run on the command line and not when accessing the page through a browser... My question is, can I open the page from a command line like a browser would??? I would like to make it a cron job, and currently I try "php update.php", but that receives the error... Is there a "http update.php" or a "get update.php" that would potentially do the same thing as a browser?

Reading From Command Line
I want to be able to have an infinite loop that is exited when the user types "q" on the command line. This is pretty trivial:

while(1)
{
    if (trim(fgets(STDIN) == "q")
    {
         exit;  }}


The loop however holds and waits for user input. I searched the php.net site and in the user comments I found this function: Code:

Read Text File Line By Line
I have been given a textfile which contains data on 1000 businesses.  Each line contains a name and a code which are seperated by a comma.  The code below allows me to display the entire textfile but I want to read in the values from each line and store these in a database.

$filename = "businesses.txt";
$fileToOpen = fopen($filename,"r");
$content = fread($fileToOpen, filesize($filename));
$content = nl2br($content);
fclose($fileToOpen);
echo($content);

Does anyone know how I can read the data in line by line so that I can store it in a database.

CGI Install Only Works From Command Line
I'm running Domino R5/Solaris 7/Intel. Any program will run on the command line, but from the browser i get "Document Contains no Data", any suggestions?

PHP Command Line Called Via Exec();
If I enter the following at the command line, I
successfully get an email sent to the correct name/email:

php /var/www/html/admin/adminscripts/welcome-cli.php
'Me' 'me@me.net' ....

Mysql Command Line Client
Is there any way of making the mysql command line client full-screen?
Sometimes it's easier to use the client than go thru php, but
since it's only about 80 characters wide that limits its usefulness.

Disable Command Line Arguments For Php-cli
i have been trying to disable the -n and -c arguments that you can use with php from the command prompt. this is because these features can effectively disable my php.ini which has a whole mess of disabled_functions (that i want to ensure remain disabled).

my best idea for going about doing this so far has been to download the source for the newest php, with hopes to compile my own which will ignore arguments other than a filename. so far the closest i have got is to the the zend_API.c which has the follow function: Code:

Segmentation Fault On Command Line
I have a script running in the wee hours via cron job. When I access the
script via browser it works like a charm. I noticed it was not succeeding
and ran it manually from the command line and it threw a Segmentation Fault.

Googling produces a ton of information on this subject but al of specific to
certain binaries and it doesn't seem germaine to solving my problem.
<?
mysql_connect('mysql.example.com', 'example', 'example');
mysql_select_db('example');
mysql_query("DELETE FROM aniSessionLog WHERE timestamp < NOW() - 900");
$rows = mysql_affected_rows();
mail('jg@example.com', 'cleaned Intranet DB', "$rows rows deleted.");
?>

Query About Command Line PHP Execution
I want to know how to execute php file from the command line.I knwo that we can do via " PHP sample.php". But if I want to send any parameters to my sample.php how can i achieve ..

MySQL Database From The Command Line
I am creating a MySQL database from the command line for the first
time. I am creating a database called 'goldminers' with 1 table called
'goldminers'. What am I doing wrong here:

mysql> use goldminers
Database changed
mysql> drop table goldminers
-> ;
Query OK, 0 rows affected (0.13 sec)

mysql> make table goldminers
-> (id int unsigned not null primary key,
-> name char(30) not null,
-> dateIssued date not null,
-> location char(20) not null,
-> comment text null,
-> url text null);
ERROR 1064: You have an error in your SQL syntax near 'make table
goldminers
(id int unsigned not null primary key,
name char(30) not n' at line 1

The reason that I didn't make the ID auto_increment is that, even
though it is generally incrementing by 1 every time, there are some
places where the database skips numbers because of goldminers who we
don't have any information on. I need my database's IDs to match the
IDs on the existing paper-version of the database.

Command Line On A Linux Machine
how I can run a PHP script from the command line on a linux machine.

Passing Parameter In Command Line
Can somebody tell me how can I pass a parameter to a PHP script in de command line?
for instance: bin]# ./print_listname 18

<?php
//$list_number = // parameter var;
echo $list_number;?>

Running PHP On Command Line & IIS Setup
I am trying to run PHP off of a IIS server....which hasn't be so pretty. I have included the path environment variable C:PHP, rebooted and started back up. Within C:PHP are two executables php-cgi.exe and php-win.exe. everything I have read online has said that all I need to do is point an environment path to this folder and everything should work. wrong!!!!

I try to run a script in dos and get php is not valid command error.....it's as if it isn't recognizing the command path. I have also set up php under properties to be opened by the CLI....so what am I doing wrong here? I'm running IIS 6 with a dedicated server.

Sed Command Fails If Line Has Quote
I'm trying to make a script that will search a list of files and remove
the following line

<meta http-equiv="Content-Type" content="text/html; charset=<?php echo
CHARSET; ?>">

I'm doing this by generating a sed command and using system to execute
it like so:

$t = sprintf("%s "s/<meta http-equiv="Content-Type" content="text/html;
charset=<?php echo CHARSET; ?>">%s/g" %s >%s", $sedPath,$str1_rpl,
$infile, $outfile);
system($t);

The problem is the quote found in the text to be replaced. sed sees it
as part of its command and ignores everything after it. For example, if
I use
<meta http-equiv="Content-Type" content=

for the search string, it works. But if I use
<meta http-equiv="Content-Type" content="

it doesn't work. I have tried escaping the quote

Php Designer 2007 Command Line
In windows XP, I want to be able to click on a .php file and have it
opened in PHP Designer 2007,
Or right-click on it and have an "edit" menu option presented to me.
I know how to tweak the registry to do this in general, but what would
the command line look like in this specific case ?

Invoking Dompdf Via The Command Line
A little question, what i can use dompdf via command line?
The syntax is:

$ php -f dompdf.php -- [options]

Can any explain me the meaning and the usage? I must pass a file .htm

Command Line PHP: Installing On Debian
I have installed PHP4.3.10 Apache2 module (on Debian linux) and my PHP
scripts work fine accessed via a browser.

What extra do I need to install to get PHP to work from the command
line?

Quote:

[windows] PHP.GTK And Popuped Command Line
i compile php.gtk file with roadsend php compiler. Is here possiblity to
have windows program without starting command line window?

Speeding Up Command Line Scripts
I have a command line script which works really fine, the only problem
is that it take *really* long for the first output to be printed on
screen.
Since I also get some HTTP headers I'm suspecting that some sort of
output buffering is used.
How can I tell PHP to flush the buffer automatically (without using
flush(); after every print or echo) and to remove the headers?

Command-line PHP And Suppressing Headers - Help
How do I suppress the lines PHP normally delivers to stdout (your browser)
if I am doing command-line PHP?

e.g.

stuff.php:

<? echo 'Hello World' ?>

Calling it from a Red Hat 7.3 command-line terminal I would do the
following:

php /my/directory/path/to/stuff.php

And I would eventually get

Hello World

But before that, an enormous amount of garbage, particularly HTTP header
information:

PHP Version 4.2.3
Content-type: text/html
Hello World

How do I strip everything out except that which I need to see while using
PHP from the command line? I am having a horrific time using TCL instead.

Syntax Check On The Command Line
It would be great if anyone could help me with this problem:
I want to check the syntax of my php code. Therefore I use the command
line function PHP -l, the structure is similar to the following code
(example from the php Manual.) The Problem is: I read from the standard
output with
echo stream_get_contents($pipes[1]);
this function is available only from PHP version 5, but I must write
for version 4.3.0. Which way could it be solved? I don't want to use a
temporary file.

<?php
$descriptorspec = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will
read from
1 => array("pipe", "w"), // stdout is a pipe that the child will
write to
2 => array("file", "/tmp/error-output.txt", "a") // stderr is a file
to write to
);

$cwd = '/tmp'
$env = array('some_option' => 'aeiou');

$process = proc_open('php', $descriptorspec, $pipes, $cwd, $env);

if (is_resource($process)) {
// $pipes now looks like this:
// 0 => writeable handle connected to child stdin
// 1 => readable handle connected to child stdout
// Any error output will be appended to /tmp/error-output.txt

fwrite($pipes[0], '<?php print_r($_ENV); ?>');
fclose($pipes[0]);

echo stream_get_contents($pipes[1]);
fclose($pipes[1]);

// It is important that you close any pipes before calling
// proc_close in order to avoid a deadlock
$return_value = proc_close($process);

echo "command returned $return_value
";
}

?>

PHP Execute Command Line Prog
Is there a cleaner way to execute a command line php script from within a web accessed php script? Im currently using exec("php foo.php"); but it seems like there should be a better way.

Running Script From The Command Line
I'm trying to set up email piping on my server and I'm getting the following error back in my email:

/bin/sh: /var/www/vhosts/example.com/httpdocs/mailcontroller.php: /usr/bin/php
: bad interpreter: No such file or directory

I think the mail alias I set up in qmail is working correctly because when it wasn't I didn't get a script error back, just a user not found error.

From the error it sounds like it's not able to find php but I've tested a script from the command line to that path and it works fine (php is running as CGI btw, rather than CLI). I'm guessing that it's making it all the way to mailcontroller.php because I don't have the /usr/bin/php path anywhere else but in that file.  Could it be that it's actually running the script but can't get to stdin?

Connect To PEAR With Command Line
I have recently installed PEAR to my base directory next to my website which wasn't a problem (i hope). What i need to do now is install the BBCodeParser package.

I need to know how to install this package using the command line.  Do i use the command prompt on my PC and if so how do i connect to the web server which is on a host not stored locally.


Copyright © 2005-08 www.BigResource.com, All rights reserved