Aesir John
Aesir John

Reputation: 55

How to replace all text after the first dot via PHP/MySQL

I would like to replace all text after the first dot using a php file. For words I use this code right now:

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "UPDATE pm_videos SET `description` = REPLACE(  `description`  ,'Facebook',  '')";
if ($conn->query($sql) === TRUE) {
    echo "Record updated successfully with Facebook, ";
} else {
    echo "Error updating record: " .  $conn->error;
}

$sql = "UPDATE pm_videos SET `description` = REPLACE(  `description` ,'Twitter',  '')";
if ($conn->query($sql) === TRUE) {
    echo "Record updated successfully with Twitter, ";
} else {
    echo "Error updating record: " .  $conn->error;
}

$conn->close()

...but I want to replace/delete all after the first dot.

For example:

Before:

Asia's Got Talent Grand Winner El Gamma Penumbra performs on ASAP stage. Subscribe to ABS-CBN Entertainment channel! - Watch the full episodes of ASAP 20 on TFC.TV and on IWANT.TV for Philippine viewers, click: Visit our official website! Facebook: Twitter: Instagram:

After:

Asia's Got Talent Grand Winner El Gamma Penumbra performs on ASAP stage.  

Upvotes: 0

Views: 217

Answers (1)

Konstantin Kulakov
Konstantin Kulakov

Reputation: 136

You can use php function strpos that find dot in string stripos(). To next you must send position to substr() where first param string, second param is start(in your case 0) and last funded position strpos.

$result = substr($string, 0, stripos($string, "."));

Upvotes: 1

Related Questions