Jump to content

strcpy/strcpy_s?


Illasera

Recommended Posts

I am looking for a function like the C language strcpy, Note that most PHP functions are taken from C , I was a bit suprised when i found out that this function isn`t included as well,I am trying to copy a partion of a string to a smaller string, I tried using for and foreach loop but they wont work well (I am mis-using them),Since its a really short function, Can someone guide me how to write it?Thanks in advance

Link to comment
Share on other sites

C has a strcpy function because the original C did not define a string as a native data type. A string was a character array, so the assignment operator did not work.In PHP, if you want to copy an entire string into a new variable, simply use the assignment operator, same as you would an integer. This is an assignment by value, not reference, so it does what you want. E.g.:$str1 = "Bob";$str2 = $str1;$str1 = "Hi, ";echo $str1 . $str2; // "Hi, Bob"If you want to copy a portion, consider substr. If you are new to the online manual, look to the left of the page I linked you to for a complete list of string-related function. If substr is not quite what you want, another function probably will be.

most PHP functions are taken from C
It is true that PHP syntax is mostly borrowed from C. PHP also interfaces with the file system much as C does (because both borrow their function names from UNIX system calls). But PHP provides a vastly greater number of built-in functions, and most of what C calls "libraries" are likewise built-in (at the sysop's discretion) and do not need to be explicitly included before a function can be called.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...