PHP Strings - ProgrammerTech
Language:

Search

PHP Strings

  • Share this:
PHP Strings

PHP String Functions

Strings In php, a string can be defined as a set of characters whose content can be changed after its creation. Whereas, php provides a set of multiple functions to perform various operations on them.

 

strlen() function

Often when working with strings, we need to know the length of the string we are working with. And this task can be done using the strlen() function, and the function of this function is to return the length of the text string.

<php
    $string = "programmer tech";
    echo strlen($string);
?>

When we run this code in the browser, the output will be

15

The variable that holds the string is wrapped inside the strlen() function to find out the length or number of characters of the string and the number of characters is 15.

 

Convert English characters PHP STRING

There are also some functions in the PHP language that convert English characters, whether from uppercase to lowercase and vice versa, we will get to know them now.

 

PHP STRTOLOWER

This function converts English letters from uppercase letters to lowercase letters (a, A). The function can be used when registering the user, as it converts uppercase letters to lowercase letters and vice versa.

<php
    $string1 = "PROGRAMMER TECH";
    echo strtolower($string1);
?>

When you run the code, the result will be

programmer tech

 

PHP STRTOUPPER

This function is the same as the previous function, but unlike the strtolower function, it converts lowercase letters to uppercase letters.

<php
    $string2 = "programmer tech";
    echo strtoupper($string2);
?>

When you run the code, the result will be

PROGRAMMER TECH

 

Replace or change the text PHP STRING

As in other programming languages, when we want to alter or change a text, there is a function that replaces or modifies the texts.

 

PHP STR_REPLACE

This function accepts three values:

  1. The text to be searched for or changed, and it can be a text string or an array, and in my case the text to be changed is Ahmed.
  2. The text to be replaced with the old text. It can also be an array or a text string. In my case, the new text is data.
  3. The name of the string on which the text change is being performed, in my case it is string.

 

<php
    $string = "Ahmed site manager programmer tech";
    $new_string = str_replace('Ahmed','data',$string);
    echo $new_string;
?>

When you run the code, the result will be.

data site manager programmer tech

 

Remove PHP HTML tags

There are two functions that can be used to prevent certain HTML tags from being executed.

These two functions can be used to delete the execution of HTML tags such as deleting the execution of these operations (<,>,$,*,%,') as well as deleting the HTML code. We know that if the text is inside a tag like H1 the text will appear heavily on the basis that this text is a title. But with the use of these functions, the text will appear as it is and does not accept any implementation of HTML codes, and these functions are used to protect variables from vulnerabilities. Like XSS exploits that allow hackers to execute JAVASCRIPT code through script tags located within html tags.

And we use filtering with these functions in input fields by users. You can follow the protection of the site from these vulnerabilities in detail from here. But those functions will print the text as it exists as <h1> programmer tech </h1>. But what if we want to permanently delete the html codes from the output here we use another function, which is PHP STRIP_TAGS.

<?php
$user_string1 = "<h1> programmer tech </h1>";
echo htmlentities($user_string1);
?>

This code will print the tag and its contents as follows

<h1> programmer tech </h1>

As for the second function

<?php
$user_string2 = "<h1> PROGRAMMER TECH </h1>";
echo specialchars($user_string2);
?>

This code will print the tag and its contents as follows.

<h1> PROGRAMMER TECH </h1>

 

PHP STRIP_TAGS

strip_tags function deletes all html codes from output and does not execute those codes. And we use this function a lot to filter data from users to not implement html code injection holes, such as html injection and xss holes.

<?php
$user_string3 = "<h1> programmer tech </h1>";
echo specialchars($user_string3);
?>

This code will only print what is inside the tag.

programmer tech

Tags:
Muhammad Alush

Muhammad Alush

إسمي محمد تركي العلوش من سورية من ريف حلب الشرقي 1994, محب للتقنية و التعلم الذاتي, و جميع خبراتي تعلمتها ذاتياً, و اللغات البرمجية التي أتقنها هي html, css, javascript, php, c, python بالإضافة إلى حبي الكبير لمجال الشبكات.

موقعي programmer-tech.com