Margus
Margus

Reputation: 83

Print only part of values or to a specific character

I have in MySQL table column "class" and in items is :

with print '<td>' .$row["klass"].'</td>'; prints whole text. Can I print somehow until to "(" ?

to display :

Upvotes: 1

Views: 33

Answers (3)

SvSven
SvSven

Reputation: 11

You could do it using the explode() function

print '<td>' . explode("(", $row["klass"])[0] . '</td>';

Upvotes: 0

davidkonrad
davidkonrad

Reputation: 85558

You could use preg_replace to remove the entire paranthesis :

function removeParanthesis($text) {
    return preg_replace('/\s*\([^)]*\)/', '', $text);
}

print '<td>'.removeParanthesis($row["klass"]).'</td>';

Would give the desired output.

Upvotes: 1

Abhik Chakraborty
Abhik Chakraborty

Reputation: 44864

You can do it mysql as

mysql> select substring_index('DS1 (1 koera toukerattavedu al.14 a.)','(',1) as klass ;
+-------+
| klass |
+-------+
| DS1   |
+-------+

So while selecting you may do as

select 
substring_index(klass,'(',1) as klass
from table_name

Upvotes: 0

Related Questions