Forum Moderators: coopster

Message Too Old, No Replies

String Manipulation Functions?

I need to know which string manipulation functions to use...

         

erikcw

3:58 am on May 23, 2004 (gmt 0)

10+ Year Member



Hi All,

Can anyone direct me to the php functions that perform the following functions:

I need to append data to the beginning and end of a string.
A find and replace function.
And a funtion that counts the number of lines (or number of returns) in a document.

Any suggestions?

Timotheos

6:01 am on May 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I need to append data to the beginning and end of a string.

$string = $data1 . "your string" . $data2;

A find and replace function.

str_replace [us3.php.net]

And a function that counts the number of lines (or number of returns) in a document.

I don't know of one but it wouldn't be that hard using some filesystem [us3.php.net] and string [us3.php.net] functions.

jamesa

7:30 am on May 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To follow up on what Timotheos siad, you could shell out and do a
wc -l
(that's minus "ell" at the end)

NickCoons

8:03 am on May 23, 2004 (gmt 0)

10+ Year Member



erikcw,

<And a funtion that counts the number of lines (or number of returns) in a document.>

You could use fread() to read the entire file into a variable, and then substr_count() to count the number of "\n" characters.

jatar_k

5:25 pm on May 23, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



$lines = file [ca.php.net]('myfile.txt');
$countlines = count [ca.php.net]($lines);
echo $countlines;

erikcw

5:55 pm on May 23, 2004 (gmt 0)

10+ Year Member



Thanks to all of you for your replies! It has been very helpfull.

As far as appending data before and after a string, it is a little more complex than just one string.

There will be a list of items that need IDs added to the front and end of each one.
So...

$list = "item1
item2
item3
item4";

AND it needs to look like this after processing:

$list = "id-item1-id
id2-item1-id2
id3-item1-id3
id1-item2-id1
id2-item2-id2
id3-item2-id3
etc...";

So I need to break this list into individual items, then append the id data (3 different versions for each item) and then return the result to a variable.

Also, is there a function that will find a word in a list and instead of replacing simplt add a modified version to the list?

Thanks!

IanKelley

10:08 pm on May 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The most straight forward way to take a found string and replace it with a modified version of that string is to use regular expressions.

See preg_replace and preg_replace_all functions.

Alternatively you could use a combination of native string functions to accomplish this but I suspect it would end up being a lot less efficient.

jamesa

11:09 pm on May 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In case you didn't know, erikcw, a complete list is of string functions can be found on this page of the php manual [php.net] (scroll down to the table of contents). Hope that helps.