Jump to content

Recommended Posts

I have a problem.  I need to replace all ‘ and ” (both forward and backwards...both single and double quotes) with ' and "

 

So, this sentence:

 

‘Our alumni are the reason we are in the position we are today,”

 

 

Would read:

 

'Our alumni are the reason we are in the position we are today,"

 

 

The problem is, I don't even know which key on the keyboards makes the marks I'm trying to remove. 

 

I will also want to addslashes to this prior to input into the database.

 

So, how do I make those marks...is preg_replace better or worse than str_replace?  Is there something better than addslashes?  Is there something that will do both at once?

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/212148-i-need-to-replace-certain-characters/
Share on other sites

I finally got this...once I learned that these little guys were called "Smart Quotes" it made it easier to deal with.

 

For anyone else who needs it...

<?php

//Include database connection details
require_once('config.php');

//Array to store validation errors
$errmsg_arr = array();

//Validation error flag
$errflag = false;

//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
	die('Failed to connect to server: ' . mysql_error());
}

//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
	die("Unable to select database");
}

//Function to sanitize values received from the form. Prevents SQL injection
function clean($str) {
	$str = @trim($str);
	if(get_magic_quotes_gpc()) {
		$str = stripslashes($str);
	}
	return mysql_real_escape_string($str);
}

//Sanitize the POST values
$sport = clean($_POST['sport']);
$headline = clean($_POST['headline']);
$story = $_POST['story'];	
$keyword = clean($_POST['keyword']);
$date = date('Y-m-d, g:i a');
    $show_on_main = clean($_POST['show_on_main']);


  $find[] = '“';  // left side double smart quote
  $find[] = 'â€';  // right side double smart quote
  $find[] = '‘';  // left side single smart quote
  $find[] = '’';  // right side single smart quote
  $find[] = '…';  // elipsis
  $find[] = '—';  // em dash
  $find[] = '–';  // en dash

  $replace[] = '\"';
  $replace[] = '\"';
  $replace[] = "\'";
  $replace[] = "\'";
  $replace[] = "...";
  $replace[] = "-";
  $replace[] = "-";


$clean_story = str_replace($find, $replace, $story);

function replace_newline($string) {
  return (string)str_replace(array("\r", "\r\n", "\n"), '', $string);
}

$cleaner_still = replace_newline($clean_story);
$cleaner_story = addslashes($cleaner_still);


	//Input Validations
if($sport == '') {
	$errmsg_arr[] = 'Sport missing';
	$errflag = true;
}
if($headline == '') {
	$errmsg_arr[] = 'Headline missing';
	$errflag = true;
}
if($clean_story == '') {
	$errmsg_arr[] = 'Story missing';
	$errflag = true;
}
	if($keyword == '') {
	$errmsg_arr[] = 'Keyword missing';
	$errflag = true;
    }
	if($show_on_main == '') {
	$errmsg_arr[] = 'show_on_main missing';
	$errflag = true;
    }

	//Check for duplicate keyword
if($login != '') {
	$qry = "SELECT * FROM stories WHERE keyword='$keyword'";
	$result = mysql_query($qry);
	if($result) {
		if(mysql_num_rows($result) > 0) {
			$errmsg_arr[] = 'That keyword  is already in use.  Please enter a different keyword.';
			$errflag = true;
		}
		@mysql_free_result($result);
	}
	else {
		die("Query failed");
	}
}

//If there are input validations, redirect back to the registration form
if($errflag) {
	$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
	session_write_close();
	header("location: story_entry.php");
	exit();
}

//Create INSERT query
$qry = "INSERT INTO stories(sport, date, headline, story, keyword, show_on_main) VALUES('$sport', NOW(),'$headline','$cleaner_story','$keyword','$show_on_main')";
$result = @mysql_query($qry);

//Check whether the query was successful or not
if($result) {
	header("location: story_entry.php");
	exit();
}else {
	die("Query failed");
}
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.