9 Replies - 222 Views - Last Post: 01 May 2013 - 10:18 PM Rate Topic: -----

#1 P.Conrad  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 10-March 13

adding quotations script

Posted 30 April 2013 - 05:52 AM

I thinking about some smart algorithm in php which would change this symbol ' for ‘ and next occurrence to ’ and so on.

So we put:

He said 'I love you'. She replied 'cool, but are you rich?'.


and we get

He said ‘I love you’. She replied ‘cool, but are you rich?’.


any smart tricks ?

Is This A Good Question/Topic? 0
  • +

Replies To: adding quotations script

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6021
  • View blog
  • Posts: 23,396
  • Joined: 23-August 08

Re: adding quotations script

Posted 30 April 2013 - 06:00 AM

How about htmlentities() with the ENT_QUOTES flag?
Was This Post Helpful? 1
  • +
  • -

#3 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3709
  • View blog
  • Posts: 5,957
  • Joined: 08-June 10

Re: adding quotations script

Posted 30 April 2013 - 06:05 AM

The algorithm for something like this isn't really that complicated. Something like this would be fine for the situation you are describing.
isLeftChar = true
FOR (each char in the string) {
    IF (this char is a single quote) {
        Use "substr_replace" to replace the quote char
        with either the left entity or right entity, based
        on the "isLeftChar" boolean.
        Then "flip" the "isLeftChar" boolean.
    }
}


Was This Post Helpful? 2
  • +
  • -

#4 P.Conrad  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 10-March 13

Re: adding quotations script

Posted 30 April 2013 - 12:34 PM

okey, thanks for algorithm alti but also my concern was how to actually code that in clever way. I actually came up with :

	private function addQuotes($str){

	$isLeft = true;
	$length = strlen($str);
	
	for($i = 0; $i < $length; $i++){
		
		if($str[$i] == '\''){
			
			if($isLeft ? $str[$i] = '#' : $str[$i] = '*');
			
			$isLeft = !$isLeft;
		}
		
	}	
	
	$str = str_replace("#", "&bdquo;", $str);
	$str = str_replace("*", "&rdquo;", $str);
	
		return $str;
		
	}


but I think its pretty brutal and doesn't let you use * and # in you string. Do you think is there a way to make it a more crafty ?
Was This Post Helpful? 0
  • +
  • -

#5 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3470
  • View blog
  • Posts: 9,973
  • Joined: 08-June 10

Re: adding quotations script

Posted 30 April 2013 - 02:26 PM

hm, why donít you use the entities in the first place? otherwise you get into trouble when there are any * or # in the text.

besides that, the ternary condition should be $str[$i] = $isLeft ? "&ldquo;" : "&bdquo;"
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6021
  • View blog
  • Posts: 23,396
  • Joined: 23-August 08

Re: adding quotations script

Posted 30 April 2013 - 02:59 PM

Damn, I totally boned that answer :/
Was This Post Helpful? 0
  • +
  • -

#7 P.Conrad  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 10-March 13

Re: adding quotations script

Posted 30 April 2013 - 03:35 PM

View PostDormilich, on 30 April 2013 - 09:26 PM, said:

hm, why don’t you use the entities in the first place? otherwise you get into trouble when there are any * or # in the text.


I just don't see how entities will help me in achieving what I want here. Don't they just change ' into & #39; ??

This post has been edited by P.Conrad: 30 April 2013 - 03:36 PM

Was This Post Helpful? 0
  • +
  • -

#8 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3470
  • View blog
  • Posts: 9,973
  • Joined: 08-June 10

Re: adding quotations script

Posted 30 April 2013 - 03:38 PM

didnít you want to change straight quotes into typographical ones?
Was This Post Helpful? 0
  • +
  • -

#9 P.Conrad  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 10-March 13

Re: adding quotations script

Posted 01 May 2013 - 06:07 PM

if you call this &bdquo; and that &rdquo; typographical quotes then yes.
Was This Post Helpful? 0
  • +
  • -

#10 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3470
  • View blog
  • Posts: 9,973
  • Joined: 08-June 10

Re: adding quotations script

Posted 01 May 2013 - 10:18 PM

yepp, the whole group around U+201C are typographical apostrophes (because there are left and right apostrophes)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1