Hướng dẫn dùng using strchr trong PHP

Hàm strchr() sẽ tìm kiếm vị trí đầu tiên xuất hiện của một kí tự hoặc một chuỗi nào đó trong chuỗi nguồn. Hàm trả về một phần của chuỗi gốc tính từ vị trí xuất hiện đầu tiên của kí tự đến vị trí cuối cùng của chuỗi gốc.

Hướng dẫn dùng using strchr trong PHP

Hướng dẫn dùng using strchr trong PHP

Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.

Cú pháp

Cú phápstrchr( $str, $char, $type);

Trong đó:

  • $str là chuỗi gốc cần xử lý.
  • $char là kí tự hoặc chuỗi cần xác định vị trí.
  • $type là tham số không bắt buộc quy định kết quả trả về của hàm:
    • Nếu $type = TRUE chuỗi trả về sẽ lấy từ đầu chuỗi gốc đến vị trí xuất hiện đầu tiên của $char trong chuỗi gốc và không bao gồm $char.
    • Nếu $type = FALSE chuỗi trả về sẽ lấy từ vị trí đầu tiên xuất hiện của $char trong chuỗi gốc đến cuối chuỗi gốc và lấy luôn cả $char.

Ví dụ

Code

$var = "something@";
$result = strchr($var, "@");
$result_2 = strchr($var, "@", true);
echo $result . "<br />";
echo $result_2;

Kết quả

@
something

Tham khảo: php.net

Cùng chuyên mục:

Alternative Way:
To get all the text before the first occurence.

-----------------------------------------------
INCLUDING A NEEDLE:

$string1 = "I need cookies & soda.";
$needle = "cookies";

//find length of the needle
$needle_len = strlen($needle);

//find postion
$position_num = strpos($string1,$needle) + $needle_len;

//cut the string
$result_string = substr("$string1",0,$position_num);

//display it
echo"$result_string"; // I need cookies

-----------------------------------------------
SHORTER VERSION:

$result_string = substr("$string1",0,strpos($string1,$needle)+strlen($needle));

echo"$result_string";//I need cookies

-----------------------------------------------
EXCLUDING THE NEEDLE:

$result_string = substr("$string1",0,strpos($string1,$needle));

echo"$result_string";// I need

-----------------------------------------------
FREE EMAIL JUNK?

This is probably useful for processing emails.
For example, someone sends email to your server from Yahoo account.
Free email always comes with wasted stuff like...
"Do you Yahoo!? The New Yahoo! Shopping - with improved product search ".
We can delete the phrase like this:

$needle="Do you Yahoo!?";

$result_string = substr("$emailstring",0,strpos($emailstring, $needle));

❮ PHP String Reference

Example

Find the first occurrence of "world" inside "Hello world!" and return the rest of the string:

<?php
echo strchr("Hello world!","world");
?>

Try it Yourself »


Definition and Usage

The strchr() function searches for the first occurrence of a string inside another string.

This function is an alias of the strstr() function.

Note: This function is binary-safe.

Note: This function is case-sensitive. For a case-insensitive search, use stristr() function.


Syntax

strchr(string,search,before_search);

Parameter Values

ParameterDescription
string Required. Specifies the string to search
search Required. Specifies the string to search for. If this parameter is a number, it will search for the character matching the ASCII value of the number
before_search Optional. A boolean value whose default is "false". If set to "true", it returns the part of the string before the first occurrence of the search parameter.


Technical Details

Return Value:Returns the rest of the string (from the matching point), or FALSE, if the string to search for is not found.
PHP Version:4+
Changelog:The before_search parameter was added in PHP 5.3

More Examples

Example

Search a string for the ASCII value of "o" and return the rest of the string:

<?php
echo strchr("Hello world!",111);
?>

Try it Yourself »

Example

Return the part of the string before the first occurence of "world":

<?php
echo strchr("Hello world!","world",true);
?>

Try it Yourself »


❮ PHP String Reference

Hướng dẫn dùng static constructor trong PHP

Tip This page describes the use of the static keyword to define static methods and properties. static can also be used to define static variables and for late static bindings. Please refer to those ...

Hướng dẫn dùng .includes trong PHP

Trang chủHướng dẫn họcHọc PHPPHP include và requireĐịnh nghĩa và cách dùng include và requireinclude hoặc require tiện lợi cho việc sử dụng những phần dùng chung, ...

Hướng dẫn dùng php lists trong PHP

Định nghĩa hàm list() trong PHPHàm list() trong PHP không thực sự là một hàm. Hàm list() được sử dụng để gán giá trị cho một danh sách biến trong một hoạt ...

Hướng dẫn dùng curlcode trong PHP

Giới ThiệuCURL là bộ thư viện được sử dụng để giúp thực hiện việc chuyển dữ liệu thông qua nhiều giao thức khác nhau (như HTTP, FPT...). Với giao thức ...

Hướng dẫn php validate executablepath macos

vscode requires path to php set for this parameter:php.validate.executablePath:so I specified this:/Applications/MAMP/bin/php/php7.0.12/bin/phpbut it isnt recognized.What is the correct path ? ...

Hướng dẫn dùng switch cade trong PHP

Trang chủHướng dẫn họcHọc PHPCâu lệnh switch caseCâu lệnh switch caseCâu lệnh switch case được sử dụng khi muốn lấy một lựa chọn trong nhiều điều kiện ...

Hướng dẫn dùng transcode trong PHP

Trong khi làm việc với PHP, chúng ta phải đối mặt với nhiều vấn đề liên quan đến Array(mảng) ví dụ như chuyển đổi một mảng thành một ...

How to get xml data from url in php?

you can get the data from the XML by using simplexml_load_file Function. Please refer this link http://php.net/manual/en/function.simplexml-load-file.php$url = ...

Hướng dẫn date_create trong php

Cú phápHàm date_create() trong PHP có cú pháp như sau:DateTime date_create ( [$time [, timezone]] ); DateTime DateTime::__construct ( [$time [, $timezone]] ); Định nghĩa và cách sử ...

What does it mean to install php?

What Do I Need?To start using PHP, you can:Find a web host with PHP and MySQL supportInstall a web server on your own PC, and then install PHP and MySQLUse a Web Host With PHP SupportIf your server ...

Hướng dẫn encapsulation in php

Khi mới làm quen với lập trình chúng ta thường bắt đầu với các ngôn ngữ như Pascal, C là những ngôn ngữ lập trình cấu trúc với việc thực hiện mã lệnh ...

Hướng dẫn dùng cos -150 trong PHP

- Dưới đây là danh sách các hàm toán học được xây dựng sẵn trong PHP. sin() - Trả về sin của một số. sinh() - Trả về sin hyperbolic của một số. cos() - Trả ...

Hướng dẫn dùng flist login trong PHP

I. Giới thiệu:Sau khi các bạn đã học các kiến thức cơ bản về PHP và MySQL thì trong bài này mình sẽ hướng dẫn các bạn xây dựng chức năng đăng nhập và ...

If else in echo php

I suspect its not allowable because I am getting Parse error: syntax error, unexpected T_IF in... error. But I couldnt find a way to accomplish my goal. Heres my code: <?php $countries = ...

Hướng dẫn dùng wexitstatus trong PHP

1. Giới thiệuMột trong các triết lý về lập trình đó là do one thing and do it well, làm một việc và làm tốt điều đó. Quản lý tiến trình ...

Hướng dẫn dùng interface meaning trong PHP

Blog Tin tức 04/06/2021 01:41Trong bài viết này chúng ta sẽ tìm hiểu về Interface trong PHP cùng một số cách đặt tên và triển khai Interface trong ngôn ngữ lập trình ...

Hướng dẫn dùng range 1 trong PHP

Hàm range() trong php sẽ tạo ra một mảng, các phần tử của mảng là một dãy kí tự do người dùng truyền vào kí tự đầu và cuối.Bài viết này được ...

How do you declare an integer variable in php?

An int is a number of the set ℤ = {..., -2, -1, 0, 1, 2, ...}. See also: Arbitrary length integer / GMP Floating point numbers Arbitrary precision / BCMathSyntax ints can be specified in ...

What is a php socket?

IntroductionSockets are used for interprocess communication. Interprocess communication is generally based on client-server model. In this case, client-server are the applications that interact with ...

Hướng dẫn tạo bảng trong php

Bài tập PHP: Tạo bảngViết PHP script để hiển thị chuỗi và giá trị bên trong bảng.PHP scriptDưới đây là phần PHP code để giải bài tập tạo bảng trong ...

Hướng dẫn php seconds since midnight

Using PHP how do you get the number of seconds elapsed since midnight of the current day?All ive tried right now is:$hour=substr(date(h:i:s),0,2); $minute=substr(date(h:i:s),3,2); echo ...

Hướng dẫn dùng what hashing trong PHP

Tìm hiểu khi sử dụng các hàm băm tạo dữ liệu lưu trữ passwordKhi lưu trữ password vào CSDL thường sẽ sử dụng các hàm băm khác nhau được hỗ trợ bởi hệ ...

Hướng dẫn usr/lib64/php/modules oci8 so

Dear all, I have a problem with OCI8 installation and I would appreciate if you could kindly guide me how to solve this issue. I started reading the following articles in order to learn how to ...

Hướng dẫn dùng fmods trong PHP

Hàm fmod () trong PHPVí dụĐịnh nghĩa và Cách sử dụngCú phápGiá trị tham sốChi tiết kỹ thuậtRelated posts:❮ Tham khảo Toán PHPVí dụTrả lại phần còn lại ...

Hướng dẫn code php wordpress

Th7 27, 2022 Hai G. 5ít nhất Đọc WordPress là nền tảng dễ sử dụng mà không cần đụng đến dòng code nào. Nhưng nếu biết cách sửa code WordPress thì còn tốt ...

Hướng dẫn phpmailer là gì

Bạn có một website bán hàng online bạn muốn nhận được thông báo khi có đơn hàng mới? Bạn là chủ một blog đang được nhiều người quan tâm và người ta ...

Why we use abstract class in php

What are the advantage if i use abstract class in php? i cant find anything good on that. I think i can easily do all work with out using the abstract class?You could, naturally. However, if there ...

Hướng dẫn tofixed php

Hướng dẫn dùng use sessions trong PHPKhái niệm CookieLưu CookieĐọc CookiePHP CookieKhái niệm về Session PHPSử dụng SessionHủy SessionCookie là mẩu tin nhỏ được lưu ...

Hướng dẫn mvc php example

Bài viết được sự cho phép của NhungdongcodevuiMô hình MVC là mô hình được sử dụng rộng rãi nhất trong việc phát triển ứng dụng web. Mặc dù vậy, hiện nay ...

Ecommerce website php source code

E Commerce Website in PHP is an online platform developed for an online transactions of goods and services. the transaction will completed trough online using mobile phones, tablet , computers and ...

Hướng dẫn dùng dimentional array trong PHP

What is a PHP Array? A PHP array is a variable that stores more than one piece of related data in a single variable. Think of an array as a box of chocolates with slots inside. The box represents the ...

How to update version php xampp

Many web developers, especially beginners, have a common question “How to update PHP version in XAMPP?”. After getting a lot of queries on the topic, we decided to write a short and simple ...

Add to cart in php - w3schools

Learn how to create a product card with CSS.Tailored Jeans$19.99Some text about the jeans. Super slim and comfy lorem ipsum lorem jeansum. Lorem jeamsun denim lorem jeansum.Try it Yourself »How To ...

Hướng dẫn dùng mktime trong PHP

Hàm mktime() sẽ lấy timestamp của thời gian được truyền vào.Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.Cú phápCú ...

Hướng dẫn dispatch php

IntroductionLaravel queues cung cấp một API thống nhất trên nhiều loại queue backend khác nhau, như Beanstalk, Amazon SQS, Redis, hoặc ngay cả cở sở dữ liệu quan hệ. ...

Hướng dẫn php net

PHP là tên gọi ngôn ngữ lập trình dạng kịch bản (ngôn ngữ script) chuyên dùng để viết thành phần server cho ứng dụng webđược phát triển bởi Rasmus Lerdorf ...