answersLogoWhite

0


Best Answer

In php to select a sub string from a string, use the command: string substr ( string string, int start [, int length]);Simple Examples:

$rest = substr("abcdef", 1); // returns "bcdef"

$rest = substr("abcdef", 1, 3); // returns "bcd"

$rest = substr("abcdef", 0, 4); // returns "abcd"

$rest = substr("abcdef", 0, 8); // returns "abcdef"

$rest = substr('abcdef', -1, 1); // returns "f"

// Accessing via curly braces is another option

$string = 'abcdef';

echo $string{0}; // returns a

echo $string{3}; // returns d

?>

User Avatar

Wiki User

14y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you select a substring from a string using PHP?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Basic Math
Related questions

How do you take substring from a given string in php?

Use the substr function. For example <?php $name= "naruto uzumaki"; echo substr($name, 0, 6) ; // The first six char are taken by this code ?> This will output naruto


How do you get the length of an ANSI string using PHP?

$string ="Guess my length"; $length = strlen($string); now the $length will have the length of the string.


How do you count the vowels in a string using PHP?

build an array of vowels then do a foreach on the array and then explode the string on the array value and the answer is -1 of the result


How do you remove whitespaces from a string in PHP?

<?php trim(ereg_replace( ' +', ' ', $extraWhiteSpace)); ?>


What is var char in php?

Varchar in SQL means string in PHP.


How to use strings with PHP and MySQL?

If you will see this example as below, this may help. <?php $string = mysql_real_escape_string($string); $sql = "insert into stringtable(mystring) values('$string')"; ?>


How do you decode to encode using md5 funcation in php?

md5() is one-way encryption method. Example: $test_string="php"; $md_encoded_string=md5($test_string); But, you can't decode the string back to php.So, if you need to check the entered string is php or not $user_entered_string=md5($_POST['user_input']); if($md_encoded_string == $user_entered_string) { echo "input matched"; }


How do you block swear words on webpages?

Using PHP for example and the string replacement attributed to replace the swear words with *** or something.


What is the database not found error in PHP with MySQL?

It would mean the database you are trying to select in your PHP script couldn't be found, you will need to check to see if you are using the correct details.


How do you reverse a string in PHP without using a function?

Without any function is impossible. So I'll assume you mean any coded function, in which case the predefined function below is your answer.$string = strrev($string);


How do you check the length of a string in PHP?

It is fairly simple to check the length of a string in PHP. All you need to do is use the function strlen(). Below is an example of how it is used. <?php $my_text = "Hello"; echo strlen($my_text); // outputs 5 ?>


How do you select data associated with a logged in user from your sql davabase using php?

Assuming you have the same table for the user-details and login. Also that you have got the login part working <?php // connection string statement // select database statement // check if user is present in db $user = 'user_name'; // put the value of username obtained from login $select = mysql_query("SELECT * FROM table_name WHERE user='$user'"); // if statement will check for single user before showing data if(($fetch = mysql_fetch_array($select))&&($select>0)&&($select<2)) { // only print the columns you want to show echo $fetch['col_1']; // where col_1 is the name of column echo $fetch['col_2']; } ?>