preg_replace: special requirements

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 829 Views - Last Post: 02 October 2011 - 07:33 AM Rate Topic: -----

#1 shadowstep0705  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 48
  • Joined: 26-May 10

preg_replace: special requirements

Posted 30 September 2011 - 07:55 AM

Hi everyone,

I'm trying to start creating plugins for Joomla, but the preg_replace counts for all php applications ofcourse.

For those who aren't confident with Joomla, this is my current code, which replaces every '{test}' in my article into 'test':
<?php

// no direct access
defined('_JEXEC') or die;

jimport('joomla.plugin.plugin');

class plgContentTest extends JPlugin
{
   public function onContentPrepare($context, &$article, &$params, $page = 0)
   {
   // Simple performance check to determine whether bot should process further.
   if (false === JString::strpos($article->text, '{test}')) {
      return true;
   }

      // expression to search for searches
      $regex = "^\{test}";
      $article->text = preg_replace($regex, 'test', $article->text);
   }   
}



Now, I want to expand that.

Imagine that I have an article with in it 'blablabla {test}text{/test} blablabla'
How can I replace '{test}tekst{/test}' by 'test' without editing the rest?

The next step (for me) is tracking every '{test}text{/test}' and placing them into an array, then obtain 'text' from it, do something with that and then replace the whole '{test}text{/test}' with the thing I did with 'text'.

I hope you guys understand it :P

Thanks already ;)

Is This A Good Question/Topic? 0
  • +

Replies To: preg_replace: special requirements

#2 Duckington  Icon User is offline

  • D.I.C Addict

Reputation: 170
  • View blog
  • Posts: 608
  • Joined: 12-October 09

Re: preg_replace: special requirements

Posted 30 September 2011 - 08:08 AM

View Postshadowstep0705, on 30 September 2011 - 08:55 AM, said:

Imagine that I have an article with in it 'blablabla {test}text{/test} blablabla'
How can I replace '{test}tekst{/test}' by 'test' without editing the rest?



What do you mean exactly? Could you give a real-world example, cos I'm not sure what you mean by that.
Was This Post Helpful? 0
  • +
  • -

#3 shadowstep0705  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 48
  • Joined: 26-May 10

Re: preg_replace: special requirements

Posted 30 September 2011 - 08:21 AM

Well, example that I type in as my article text:

Hello, my name is Shadowstep
This is my first article and a {test}blablabla{/test}

The '{test}blablabla{/test}' should be replaced by just 'test'.
This is useless at it's own ofcourse, but I would like to know how to do that in order the finish the last step :)
Was This Post Helpful? 0
  • +
  • -

#4 Duckington  Icon User is offline

  • D.I.C Addict

Reputation: 170
  • View blog
  • Posts: 608
  • Joined: 12-October 09

Re: preg_replace: special requirements

Posted 30 September 2011 - 08:26 AM

Well you could do something like:

$pattern = "/{test}(.*?){\/test}/";
$replace = "test";

$text = preg_replace($pattern, $replace, $text);

Off the top of my head I think that would work.

This post has been edited by Duckington: 30 September 2011 - 08:27 AM

Was This Post Helpful? 1
  • +
  • -

#5 shadowstep0705  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 48
  • Joined: 26-May 10

Re: preg_replace: special requirements

Posted 30 September 2011 - 09:36 AM

View PostDuckington, on 30 September 2011 - 04:26 PM, said:

Well you could do something like:

$pattern = "/{test}(.*?){\/test}/";
$replace = "test";

$text = preg_replace($pattern, $replace, $text);

Off the top of my head I think that would work.


Doesn't work :(
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2914
  • View blog
  • Posts: 10,092
  • Joined: 08-August 08

Re: preg_replace: special requirements

Posted 30 September 2011 - 10:05 AM

This link produces this code:
<?php
  # URL that generated this code:
  # http://txt2re.com/index-php.php3?s={test}blablabla{/test}&-4&-3&2

  $txt='{test}blablabla{/test}';

  $re1='(\\{test\\})';	# Curly Braces 1
  $re2='((?:[a-z][a-z0-9_]*))';	# Variable Name 1
  $re3='(\\{\\/test\\})';	# Curly Braces 2

  if ($c=preg_match_all ("/".$re1.$re2.$re3."/is", $txt, $matches))
  {
      $cbraces1=$matches[1][0];
      $var1=$matches[2][0];
      $cbraces2=$matches[3][0];
      print "($cbraces1) ($var1) ($cbraces2) \n";
  }

  #-----
  # Paste the code into a new php file. Then in Unix:
  # $ php x.php 
  #-----
?>

Was This Post Helpful? -1
  • +
  • -

#7 kallanar  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 5
  • Joined: 30-September 11

Re: preg_replace: special requirements

Posted 30 September 2011 - 10:47 PM

Hmm.. This should work:

$pattern = "/\{test\}.*\{\/test\}/Us";

Was This Post Helpful? 1
  • +
  • -

#8 shadowstep0705  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 48
  • Joined: 26-May 10

Re: preg_replace: special requirements

Posted 01 October 2011 - 12:24 AM

View Postkallanar, on 01 October 2011 - 06:47 AM, said:

Hmm.. This should work:

$pattern = "/\{test\}.*\{\/test\}/Us";


Have you tested it, it doesn't give any result to me :(

<?php

// no direct access
defined('_JEXEC') or die;

jimport('joomla.plugin.plugin');

class plgContentTest extends JPlugin
{
   public function onContentPrepare($context, &$article, &$params, $page = 0)
   {
   // Simple performance check to determine whether bot should process further.
   if (false === JString::strpos($article->text, '{test}')) {
      return true;
   }

	
  $pattern = "/\{test\}.*\{\/test\}/Us";
  $article->text = preg_replace($pattern, 'test', $article->text);
 

   }   
}

Was This Post Helpful? -1
  • +
  • -

#9 kallanar  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 5
  • Joined: 30-September 11

Re: preg_replace: special requirements

Posted 01 October 2011 - 12:42 AM

<?
$text = "How can I replace '{test}tekst{/test}' by 'test' without editing the rest?";
$pattern = "/\{test\}.*\{\/test\}/Us";
$replace = "test";
$text = preg_replace($pattern, $replace, $text);

echo $text;


This peace of code returned:
How can I replace 'test' by 'test' without editing the rest?
Was This Post Helpful? 1
  • +
  • -

#10 shadowstep0705  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 48
  • Joined: 26-May 10

Re: preg_replace: special requirements

Posted 01 October 2011 - 02:07 AM

View Postkallanar, on 01 October 2011 - 08:42 AM, said:

<?
$text = "How can I replace '{test}tekst{/test}' by 'test' without editing the rest?";
$pattern = "/\{test\}.*\{\/test\}/Us";
$replace = "test";
$text = preg_replace($pattern, $replace, $text);

echo $text;


This peace of code returned:
How can I replace 'test' by 'test' without editing the rest?


Okay, it works now :D

Now, next step: how can I get what is between the {test} and {/test}?
Was This Post Helpful? 0
  • +
  • -

#11 kallanar  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 5
  • Joined: 30-September 11

Re: preg_replace: special requirements

Posted 01 October 2011 - 05:21 AM

View Postshadowstep0705, on 01 October 2011 - 02:07 AM, said:

Now, next step: how can I get what is between the {test} and {/test}?


You should use preg_match_all() function for that.

$pattern = "/\{test\}(.*)\{\/test\}/Us";
preg_match_all($pattern, $text, $matches);


You will get an array $matches[0] with all matched strings and $matches[1] with what is between {test} and {/test}.
Was This Post Helpful? 1
  • +
  • -

#12 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2914
  • View blog
  • Posts: 10,092
  • Joined: 08-August 08

Re: preg_replace: special requirements

Posted 01 October 2011 - 06:45 AM

Giving out negative reps when to people who've practically handed you the solution is not smart.

If you had bothered to try the code I provided you'd have seen that the expression produces exactly the result you're looking for. It puts it in $match[2][0] or $var1. Even better, the link shows you how to get it! All you need to do from there is use the expression.
Was This Post Helpful? 1
  • +
  • -

#13 shadowstep0705  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 48
  • Joined: 26-May 10

Re: preg_replace: special requirements

Posted 01 October 2011 - 08:42 AM

View Postkallanar, on 01 October 2011 - 01:21 PM, said:

View Postshadowstep0705, on 01 October 2011 - 02:07 AM, said:

Now, next step: how can I get what is between the {test} and {/test}?


You should use preg_match_all() function for that.

$pattern = "/\{test\}(.*)\{\/test\}/Us";
preg_match_all($pattern, $text, $matches);


You will get an array $matches[0] with all matched strings and $matches[1] with what is between {test} and {/test}.


Ok, thanks for your help, but I'm struck at the fact, that now I have to change each string between {test} and {/test} to test2 for example, and then replace the whole article text with it so that there is now 'test2' between each {test} and {/test}
Was This Post Helpful? 0
  • +
  • -

#14 shadowstep0705  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 48
  • Joined: 26-May 10

Re: preg_replace: special requirements

Posted 02 October 2011 - 03:58 AM

Bump
Was This Post Helpful? 0
  • +
  • -

#15 kallanar  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 5
  • Joined: 30-September 11

Re: preg_replace: special requirements

Posted 02 October 2011 - 05:59 AM

View Postshadowstep0705, on 01 October 2011 - 08:42 AM, said:

Ok, thanks for your help, but I'm struck at the fact, that now I have to change each string between {test} and {/test} to test2 for example, and then replace the whole article text with it so that there is now 'test2' between each {test} and {/test}


Ok, as I understand, you want to get all strings between {test} and {/test}, do something with them, and replace them with new text.

I would do something like this:
$pattern = "/\{test\}(.*)\{\/test\}/Us";
preg_match_all($pattern, $text, $matches);

foreach($matches[0] as $key => $match)
{
    $matches[2][$key] = doSomething($matches[1][$key]);
}
$text = str_replace($matches[0], $matches[2], $text);

function doSomething($txt)
{
    return '{test}'.'test2'.'{/test}';
}

Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2