Split text by symbol: Difference between revisions
Jump to navigation
Jump to search
m
no edit summary
mNo edit summary |
|||
| Line 1: | Line 1: | ||
Split text by symbol (another string, also called delimiter or separator) | Split text by symbol (another string, also called delimiter or separator) | ||
== Example data == | |||
Task: Split the position e.g. 25.040215, 121.512532 by the comma ( , ) to obtain the latitude (1st string) and longitude (2nd string). | |||
Expected output: | |||
* 25.040215 | |||
* 121.512532 | |||
== Approaches == | == Approaches == | ||
| Line 7: | Line 14: | ||
* PHP: [http://php.net/explode explode] or [http://php.net/manual/en/function.preg-split.php preg_split] function | * PHP: [http://php.net/explode explode] or [http://php.net/manual/en/function.preg-split.php preg_split] function | ||
=== MySQL | === Split text by symbol in MySQL === | ||
{{exclaim}} Only one symbol was allowed. The following query will get the last element if there are more than two symbols. | {{exclaim}} Only one symbol was allowed. The following query will get the last element if there are more than two symbols. | ||
# to get the first string ( 25.040215 ): {{code | code=SELECT TRIM(SUBSTRING_INDEX('25.040215, 121.512532' ,',', <span style="color:red">1</span>))}} | # to get the first string ( 25.040215 ): {{code | code=SELECT TRIM(SUBSTRING_INDEX('25.040215, 121.512532' ,',', <span style="color:red">1</span>))}} | ||
| Line 28: | Line 29: | ||
</pre> | </pre> | ||
=== Excel | === Split text by symbol in Excel === | ||
Comparison of approaches | Comparison of approaches | ||
<table border="1" class="wikitable sortable"> | <table border="1" class="wikitable sortable"> | ||
| Line 83: | Line 84: | ||
}} | }} | ||
=== PHP | === Split text by symbol in PHP === | ||
PHP script | PHP script | ||
<pre> | <pre> | ||