0

I read on the forum about php function LTRIM, I used it in my code but still getting white space on the left side of the text, here is an example of my code, anyone could tell me what's wrong with the LTRIM function below?

$sql = "UPDATE content SET text = LTRIM('$content21') WHERE element_id = '21' ";
TN888
  • 7,659
  • 9
  • 48
  • 84
Michel
  • 183
  • 2
  • 15

3 Answers3

0

Try to trim before the query like

$content21 = ltrim($content21);
$sql = "UPDATE content SET text = '$content21' WHERE element_id = '21' ";
GautamD31
  • 28,552
  • 10
  • 64
  • 85
  • That wont work either. You are not refering to `$content21`'s value but `$content21` as a string itself – Sebastien Jul 24 '13 at 12:26
0
$sql = "UPDATE `content` SET `text` = LTRIM('".$content21."') WHERE `element_id` = 21 ";

extract the var from string

edit based on comment, as there are tons of solutions for that:

$sql = "UPDATE `content` SET `text` = LTRIM('".htmlspecialchars($content21, ENT_QUOTES)."') WHERE `element_id` = 21 ";
glglgl
  • 89,107
  • 13
  • 149
  • 217
de_nuit
  • 650
  • 7
  • 13
0

this should work:

$sql = "UPDATE content SET text = ".ltrim($content21)." WHERE element_id = '21'";
Sebastien
  • 1,308
  • 2
  • 15
  • 39
  • It removes all the text after the ' so for instance I tried test's 123, in the DB, there is only: test – Michel Jul 24 '13 at 12:27
  • Also, it does remove the white space but it removes all the text after the apostrophe ' – Michel Jul 24 '13 at 12:28
  • Well this is exactly what `ltrim()` does it trim the left part of the string. – Sebastien Jul 24 '13 at 12:31
  • If you want to remove only the spaces you could use `$sql = "UPDATE content SET text = ".str_replace(" ", "", $content21)." WHERE element_id = 21";` – Sebastien Jul 24 '13 at 12:34
  • I thought the ltrim removes white space only? It removes any sentences after an apostrophe ' inside the DB so for instance "john's hat" is saved as "john" in the db – Michel Jul 24 '13 at 12:44
  • What I wanted to say is that `ltrim($str, $char)` only remove from the left of the file and it does'nt remove only whitespaces you can Specified characters to be removed. – Sebastien Jul 24 '13 at 15:21
  • And your `'` is marking the end of the sql string you'll have to use another function to take care of the `'` in `"John's"`. – Sebastien Jul 24 '13 at 15:24
  • or you could use `ltrim($content21, "'");` – Sebastien Jul 24 '13 at 15:25