PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, November 20, 2022

[FIXED] How to replace some special characters?

 November 20, 2022     php, preg-replace     No comments   

Issue

I have a PHP code that will connect to a database, select a table and then get data from it. In one of my colums (That named title), i have data looks like: Hello World - This/That is (Test) But i want to convert this to this: hello-world-this-that-is-test Because i want to use this kind of text in my page's URL.

I mean i need to

  1. Remove spaces
  2. Remove double or multiple dashes
  3. Remove slash and backslash
  4. Remove brackets like ( and )

and want to replace all of them with only one dash as i wrote above. in other word i want to delete all special characters and convert them to only one dash

Currently my code can remove space and double dashes, but it can't remove slashes and brackets. here is my code:

<?php 
 define('DBHOST','localhost');
 define('DBUSER','user');
 define('DBPASS','pass');
 define('DBNAME','name');

$db = mysqli_connect(DBHOST, DBUSER, DBPASS, DBNAME);
$db->set_charset('utf8mb4');
if(!$db){
 die( "Sorry! There seems to be a problem connecting to our database.");
}

$myFile = "sitemap.xml";
$fh = fopen($myFile, 'w') or die("can't open file"); 
$rss_txt = null;

$rss_txt .= '<?xml version="1.0" encoding="UTF-8"?>';
$rss_txt .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">';

$query = mysqli_query($db, "SELECT * FROM posts_articles ORDER BY post_id ASC");
while($values_query = mysqli_fetch_assoc($query)) {
    $rss_txt .= '<url>';
    $rss_txt .= '<loc>https://example.com/blogs/'.$values_query["post_id"].'/'.$var = preg_replace("/[\s-]+/", "-", $values_query["title"]).'</loc>';
    $rss_txt .= '<changefreq>weekly</changefreq>';
    $rss_txt .= '<priority>0.7</priority>';
    $rss_txt .= '</url>';
}

$rss_txt .= '</urlset>';

fwrite($fh, $rss_txt);
fclose($fh);

?>

OK. This part of my code have to get title '.$var = preg_replace("/[\s-]+/", "-", $values_query["title"]).' this code can remove space and multi dashes to only one dash. USEFUL! But if title column have brackets or slashes, it can't remove them and my code will fail.

I'm very very new to PHP and SQL so don't blame me. i have to start learning but after reading over 50 articles, i couldn't find anything useful to do what i want.


Solution

Here's a solution that uses three separate regular expressions to do successive transformations to the string:


<?php

$regex1 = "/[^0-9a-z\s]+/i";        // Search for everything that is not alphanumeric or whitespace
$regex2 = "/\s+/";                  // Search for whitespace
$regex3 = "/-+$/";                  // search for trailing dashes

$text = "Hello World - This/That is (Test)";

// replace in turn, non-alpha with space, one or more spaces with dash,
// one or more trailing dashes with nothing 
$text2 = preg_replace([$regex1, $regex2, $regex3],[' ','-',''], $text);

// Switch everything to lower case
$text2 = strtolower($text2);

echo $text2;       // hello-world-this-that-is-test

Demo:https://3v4l.org/Pe5hC

(The regex purists might be able to condense this further, but this is clear and easily modifiable)



Answered By - Tangentially Perpendicular
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing