Call a Function from a String in PHP

AmitDiwan
Updated on 13-Oct-2020 08:03:11

2K+ Views

To call a function from a string stored in a variable, use $func.The syntax is as follows $func=’anyFunctionName’;ExampleThe PHP code is as follows  Live Demo OutputThis will produce the following outputCalling hello method.

Bold and Simple Text in the Same Line Using Echo in PHP

AmitDiwan
Updated on 13-Oct-2020 08:01:37

3K+ Views

In order to get bold text, you need to use font-weight and to get the simple text you can use tag. All this gets added in the echo.ExampleThe PHP code is as follows Live Demo OutputHere is the snapshot of sample output.This is my first PHP Program

Handle Multi-Byte UTF-8 Strings in JavaScript

AmitDiwan
Updated on 13-Oct-2020 07:59:11

268 Views

For this, at first use preg_split() and set the function with this −'//u'After setting like above, the empty delimiter issue will get fixed, since we added '//u' above.ExampleThe PHP code is as follows Live Demo OutputThis will produce the following output −Array ( ) Array ( [0] => J [1] => o [2] => h [3] => n [4] => [5] => S [6] => m [7] => i [8] => t [9] => h )

Find Matching Values Using Foreach Loop with Two Arrays in PHP

AmitDiwan
Updated on 13-Oct-2020 07:50:00

3K+ Views

Let’s say we have the following two arrays $firstArray=array(10,20,30,40,50); $secondArray=array(100,80,30,40,90);We need to find the matching i.e. the output should be 30 40ExampleThe PHP code is as follows Live Demo OutputThis will produce the following output The matching result is=30 The matching result is=40

Get Value from JSON in PHP

AmitDiwan
Updated on 13-Oct-2020 07:44:30

3K+ Views

To get a value from JSON, use json_decode(). Let’s say the following is our JSON $detailsJsonObject = '{"details":[{"name":"John","subjectDetails":{"subjectId":"101","subjectName":"PHP","marks":"58", "teacherName":"Bob"}}]}';  We need to fetch specific values i.e. Subject Name, Marks, etc.ExampleThe PHP code is as follows  Live Demo OutputThis will produce the following outputThe Subject Name is=PHP The Teacher Name is=Bob

Get Last Part of URL in PHP

AmitDiwan
Updated on 13-Oct-2020 07:40:18

2K+ Views

To get the last part of URL, use preg_match() in PHP. Let’s say the following is our input URL $websiteAddress='https://www.tutorialspoint.com/java/java_questions_answers/9989809898';We need to get the following output, with only the last part, which is a number 9989809898'ExampleFollowing is the PHP code to get the last part of URL  Live Demo OutputThis will produce the following outputThe result is as follows:82345999

Printing Unwanted Characters with PHP preg_match

AmitDiwan
Updated on 13-Oct-2020 07:38:47

149 Views

To print unwanted characters, use preg_match_all(). Let’s say the following is our string with some special characters −$sentence= "M-y Name/i_s John+Doe";We want the output to display only the special characters from the above string i.e.-/_+ExampleThe PHP code is as follows Live Demo OutputThis will produce the following outputThe original value is=M-y Name/i_s John+Doe The unwanted characters are as follows= -/_+

Search for PHP Array Element Containing String

AmitDiwan
Updated on 13-Oct-2020 07:36:19

471 Views

Let’s say we have the following array $subject= array('JavaScript','Java','PHP language','Python Language');From the above array, we need to fetch values with the following text $valueToSearch= 'Language';For such match, use preg_match() in PHP.ExampleThe PHP code is as follows Live Demo OutputThis will produce the following outputArray ( [2] => PHP language [3] => Python Language )

Remove Comma from a String in PHP

AmitDiwan
Updated on 13-Oct-2020 07:34:08

10K+ Views

To remove comma, you can replace. To replace, use str_replace() in PHP.Let’s say the following is our input string with comma $name="John,Smith";We want the output after removing comma from the above string is JohnSmithExampleThe PHP code is as follows Live Demo OutputThis will produce the following outputThe actual value is=John,Smith After removing the comma(,)=JohnSmith

PHP preg_split with Hyphen

AmitDiwan
Updated on 13-Oct-2020 07:32:07

480 Views

Use preg_split() in PHP and split with hyphen. Let’s say the following is our input value with numbers and string separated with hyphen $values ="ABC-DEF IJKL-3553435-8990987876";We want the output to beArray ( [0] => ABC-DEF IJKL [1] => 3553435 [2] => 8990987876 )ExampleThe PHP code is as follows Live Demo OutputThis will produce the following output Array ( [0] => ABC-DEF IJKL [1] => 3553435 [2] => 8990987876 )

Advertisements