Using preg_replace in laravel










0















I have database data like text [mydata] some text [otherdata] some more text and I want to replace those [mydata] , [otherdata] with dynamic info such as category title for instance.



So the result of my data would be like:



text Category Title some text Category Desc some more text


instead of



text [mydata] some text [otherdata] some more text


I think that can be happen by preg_replace but not quite sure.



code



View::composer('*', function ($view) 
$notes = Notes::all();
foreach($notes as $note)
$descrip= $note->description;


$view->with('descrip', $descrip);
);


More



So basically $note->description content is this data:



text [mydata] some text [otherdata] some more text


I want to replace those elements by data from categories table.



Any idea?



Update



well i was digging and get code below (useing str_replace) however it has some issues,



View::composer('*', function ($view) 
//getting categories and pull out the values i want to use as `[....]`
$catC = Category::all();
foreach($catC as $catD)
$meta_title = $catD->title;
$meta_desc = $catD->meta_description;
$meta_tags = $catD->meta_tags;

//replace those categories values with element in database `[......]`
$seotemplates = SeoTemplate::all();
foreach($seotemplates as $seotemplate)
$myCustom = str_replace_array('[cattitle]', [$meta_title, $meta_desc, $meta_tags], $seotemplate->categories_desc_template);


$view->with('myCustom', $myCustom);
);


Issues



  1. I can only get [cattitle] but what about [catmeta] & [cattags]?


  2. $meta_title will always return first value from categories table (for example if i'm visiting HP category page it return ACER as [cattitle] or any other category pages i visit. It's always ACER

Update 2



I solved the issue 1 of my first update but still issue 2 remained, here is updated code



View::composer('*', function ($view) 
//categories
$catC = Category::all();
$catD = ;
foreach($catC as $catD)
$catD = [
$cattitle = $catD->title,
$catmeta = $catD->meta_tags,
$catdesc = $catD->meta_description
];

$a1 = array("[cattitle]","[catmeta]","[catdesc]");

$seotemplates = SeoTemplate::all();
foreach($seotemplates as $seotemplate)
$myCustom = str_replace($a1, $catD, $seotemplate->categories_desc_template);


$view->with('myCustom', $myCustom);
);


Current issue:





  1. $meta_title will always return first value from categories table (for example if i'm visiting HP category page it return ACER as
    [cattitle] or any other category pages i visit. It's always ACER










share|improve this question
























  • You don't need regex. You can use str_replace.

    – user3783243
    Nov 16 '18 at 1:29











  • @user3783243 I've tried that It has some issue i'll update my question.

    – mafortis
    Nov 16 '18 at 1:37











  • @user3783243 updated

    – mafortis
    Nov 16 '18 at 1:39











  • Looks like there's some variable naming issue. Your $catD is not updated because it refers to the variable within the foreach scope. Define the outside variable as $catE and then inside the loop use $catE = [ $cattitle = $catD->title, ... ] ?

    – hktang
    Nov 16 '18 at 3:34












  • @hktang regarding to $catE result is the same as before i still get acer info instead of hp

    – mafortis
    Nov 16 '18 at 8:48
















0















I have database data like text [mydata] some text [otherdata] some more text and I want to replace those [mydata] , [otherdata] with dynamic info such as category title for instance.



So the result of my data would be like:



text Category Title some text Category Desc some more text


instead of



text [mydata] some text [otherdata] some more text


I think that can be happen by preg_replace but not quite sure.



code



View::composer('*', function ($view) 
$notes = Notes::all();
foreach($notes as $note)
$descrip= $note->description;


$view->with('descrip', $descrip);
);


More



So basically $note->description content is this data:



text [mydata] some text [otherdata] some more text


I want to replace those elements by data from categories table.



Any idea?



Update



well i was digging and get code below (useing str_replace) however it has some issues,



View::composer('*', function ($view) 
//getting categories and pull out the values i want to use as `[....]`
$catC = Category::all();
foreach($catC as $catD)
$meta_title = $catD->title;
$meta_desc = $catD->meta_description;
$meta_tags = $catD->meta_tags;

//replace those categories values with element in database `[......]`
$seotemplates = SeoTemplate::all();
foreach($seotemplates as $seotemplate)
$myCustom = str_replace_array('[cattitle]', [$meta_title, $meta_desc, $meta_tags], $seotemplate->categories_desc_template);


$view->with('myCustom', $myCustom);
);


Issues



  1. I can only get [cattitle] but what about [catmeta] & [cattags]?


  2. $meta_title will always return first value from categories table (for example if i'm visiting HP category page it return ACER as [cattitle] or any other category pages i visit. It's always ACER

Update 2



I solved the issue 1 of my first update but still issue 2 remained, here is updated code



View::composer('*', function ($view) 
//categories
$catC = Category::all();
$catD = ;
foreach($catC as $catD)
$catD = [
$cattitle = $catD->title,
$catmeta = $catD->meta_tags,
$catdesc = $catD->meta_description
];

$a1 = array("[cattitle]","[catmeta]","[catdesc]");

$seotemplates = SeoTemplate::all();
foreach($seotemplates as $seotemplate)
$myCustom = str_replace($a1, $catD, $seotemplate->categories_desc_template);


$view->with('myCustom', $myCustom);
);


Current issue:





  1. $meta_title will always return first value from categories table (for example if i'm visiting HP category page it return ACER as
    [cattitle] or any other category pages i visit. It's always ACER










share|improve this question
























  • You don't need regex. You can use str_replace.

    – user3783243
    Nov 16 '18 at 1:29











  • @user3783243 I've tried that It has some issue i'll update my question.

    – mafortis
    Nov 16 '18 at 1:37











  • @user3783243 updated

    – mafortis
    Nov 16 '18 at 1:39











  • Looks like there's some variable naming issue. Your $catD is not updated because it refers to the variable within the foreach scope. Define the outside variable as $catE and then inside the loop use $catE = [ $cattitle = $catD->title, ... ] ?

    – hktang
    Nov 16 '18 at 3:34












  • @hktang regarding to $catE result is the same as before i still get acer info instead of hp

    – mafortis
    Nov 16 '18 at 8:48














0












0








0








I have database data like text [mydata] some text [otherdata] some more text and I want to replace those [mydata] , [otherdata] with dynamic info such as category title for instance.



So the result of my data would be like:



text Category Title some text Category Desc some more text


instead of



text [mydata] some text [otherdata] some more text


I think that can be happen by preg_replace but not quite sure.



code



View::composer('*', function ($view) 
$notes = Notes::all();
foreach($notes as $note)
$descrip= $note->description;


$view->with('descrip', $descrip);
);


More



So basically $note->description content is this data:



text [mydata] some text [otherdata] some more text


I want to replace those elements by data from categories table.



Any idea?



Update



well i was digging and get code below (useing str_replace) however it has some issues,



View::composer('*', function ($view) 
//getting categories and pull out the values i want to use as `[....]`
$catC = Category::all();
foreach($catC as $catD)
$meta_title = $catD->title;
$meta_desc = $catD->meta_description;
$meta_tags = $catD->meta_tags;

//replace those categories values with element in database `[......]`
$seotemplates = SeoTemplate::all();
foreach($seotemplates as $seotemplate)
$myCustom = str_replace_array('[cattitle]', [$meta_title, $meta_desc, $meta_tags], $seotemplate->categories_desc_template);


$view->with('myCustom', $myCustom);
);


Issues



  1. I can only get [cattitle] but what about [catmeta] & [cattags]?


  2. $meta_title will always return first value from categories table (for example if i'm visiting HP category page it return ACER as [cattitle] or any other category pages i visit. It's always ACER

Update 2



I solved the issue 1 of my first update but still issue 2 remained, here is updated code



View::composer('*', function ($view) 
//categories
$catC = Category::all();
$catD = ;
foreach($catC as $catD)
$catD = [
$cattitle = $catD->title,
$catmeta = $catD->meta_tags,
$catdesc = $catD->meta_description
];

$a1 = array("[cattitle]","[catmeta]","[catdesc]");

$seotemplates = SeoTemplate::all();
foreach($seotemplates as $seotemplate)
$myCustom = str_replace($a1, $catD, $seotemplate->categories_desc_template);


$view->with('myCustom', $myCustom);
);


Current issue:





  1. $meta_title will always return first value from categories table (for example if i'm visiting HP category page it return ACER as
    [cattitle] or any other category pages i visit. It's always ACER










share|improve this question
















I have database data like text [mydata] some text [otherdata] some more text and I want to replace those [mydata] , [otherdata] with dynamic info such as category title for instance.



So the result of my data would be like:



text Category Title some text Category Desc some more text


instead of



text [mydata] some text [otherdata] some more text


I think that can be happen by preg_replace but not quite sure.



code



View::composer('*', function ($view) 
$notes = Notes::all();
foreach($notes as $note)
$descrip= $note->description;


$view->with('descrip', $descrip);
);


More



So basically $note->description content is this data:



text [mydata] some text [otherdata] some more text


I want to replace those elements by data from categories table.



Any idea?



Update



well i was digging and get code below (useing str_replace) however it has some issues,



View::composer('*', function ($view) 
//getting categories and pull out the values i want to use as `[....]`
$catC = Category::all();
foreach($catC as $catD)
$meta_title = $catD->title;
$meta_desc = $catD->meta_description;
$meta_tags = $catD->meta_tags;

//replace those categories values with element in database `[......]`
$seotemplates = SeoTemplate::all();
foreach($seotemplates as $seotemplate)
$myCustom = str_replace_array('[cattitle]', [$meta_title, $meta_desc, $meta_tags], $seotemplate->categories_desc_template);


$view->with('myCustom', $myCustom);
);


Issues



  1. I can only get [cattitle] but what about [catmeta] & [cattags]?


  2. $meta_title will always return first value from categories table (for example if i'm visiting HP category page it return ACER as [cattitle] or any other category pages i visit. It's always ACER

Update 2



I solved the issue 1 of my first update but still issue 2 remained, here is updated code



View::composer('*', function ($view) 
//categories
$catC = Category::all();
$catD = ;
foreach($catC as $catD)
$catD = [
$cattitle = $catD->title,
$catmeta = $catD->meta_tags,
$catdesc = $catD->meta_description
];

$a1 = array("[cattitle]","[catmeta]","[catdesc]");

$seotemplates = SeoTemplate::all();
foreach($seotemplates as $seotemplate)
$myCustom = str_replace($a1, $catD, $seotemplate->categories_desc_template);


$view->with('myCustom', $myCustom);
);


Current issue:





  1. $meta_title will always return first value from categories table (for example if i'm visiting HP category page it return ACER as
    [cattitle] or any other category pages i visit. It's always ACER







php laravel preg-replace






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 2:48







mafortis

















asked Nov 16 '18 at 0:48









mafortismafortis

1,179943




1,179943












  • You don't need regex. You can use str_replace.

    – user3783243
    Nov 16 '18 at 1:29











  • @user3783243 I've tried that It has some issue i'll update my question.

    – mafortis
    Nov 16 '18 at 1:37











  • @user3783243 updated

    – mafortis
    Nov 16 '18 at 1:39











  • Looks like there's some variable naming issue. Your $catD is not updated because it refers to the variable within the foreach scope. Define the outside variable as $catE and then inside the loop use $catE = [ $cattitle = $catD->title, ... ] ?

    – hktang
    Nov 16 '18 at 3:34












  • @hktang regarding to $catE result is the same as before i still get acer info instead of hp

    – mafortis
    Nov 16 '18 at 8:48


















  • You don't need regex. You can use str_replace.

    – user3783243
    Nov 16 '18 at 1:29











  • @user3783243 I've tried that It has some issue i'll update my question.

    – mafortis
    Nov 16 '18 at 1:37











  • @user3783243 updated

    – mafortis
    Nov 16 '18 at 1:39











  • Looks like there's some variable naming issue. Your $catD is not updated because it refers to the variable within the foreach scope. Define the outside variable as $catE and then inside the loop use $catE = [ $cattitle = $catD->title, ... ] ?

    – hktang
    Nov 16 '18 at 3:34












  • @hktang regarding to $catE result is the same as before i still get acer info instead of hp

    – mafortis
    Nov 16 '18 at 8:48

















You don't need regex. You can use str_replace.

– user3783243
Nov 16 '18 at 1:29





You don't need regex. You can use str_replace.

– user3783243
Nov 16 '18 at 1:29













@user3783243 I've tried that It has some issue i'll update my question.

– mafortis
Nov 16 '18 at 1:37





@user3783243 I've tried that It has some issue i'll update my question.

– mafortis
Nov 16 '18 at 1:37













@user3783243 updated

– mafortis
Nov 16 '18 at 1:39





@user3783243 updated

– mafortis
Nov 16 '18 at 1:39













Looks like there's some variable naming issue. Your $catD is not updated because it refers to the variable within the foreach scope. Define the outside variable as $catE and then inside the loop use $catE = [ $cattitle = $catD->title, ... ] ?

– hktang
Nov 16 '18 at 3:34






Looks like there's some variable naming issue. Your $catD is not updated because it refers to the variable within the foreach scope. Define the outside variable as $catE and then inside the loop use $catE = [ $cattitle = $catD->title, ... ] ?

– hktang
Nov 16 '18 at 3:34














@hktang regarding to $catE result is the same as before i still get acer info instead of hp

– mafortis
Nov 16 '18 at 8:48






@hktang regarding to $catE result is the same as before i still get acer info instead of hp

– mafortis
Nov 16 '18 at 8:48













1 Answer
1






active

oldest

votes


















0














I see a couple of issues with your code that I think will help with getting the solution that you are after.



First like mentioned by @hktang said in his comment you are duplicating your variables and then assigning it's value over and over again instead of adding to it. Try this:



$catE = ;
foreach($catC as $catD)
$catE = [
$cattitle = $catD->title,
$catmeta = $catD->meta_tags,
$catdesc = $catD->meta_description
];



Second you are also resetting the value of $myCustom with each loop of the for each. Try this:



$myCustom = 
foreach($seotemplates as $seotemplate)
$myCustom = str_replace($a1, $catE, $seotemplate->categories_desc_template);



This should result in you getting an array of seotemplates with the values replaced but I have not testing this.






share|improve this answer

























  • regarding to $catE i'm getting Array to string conversion and i think $myCustom isn't right either should be equal to ; or '';

    – mafortis
    Nov 16 '18 at 8:47











  • Yeah sorry you are right. I have updated my code.

    – Josh
    Nov 16 '18 at 8:50











  • shouldn't i get catE in str_replace istead of catD?

    – mafortis
    Nov 16 '18 at 9:08











  • and oh, $catE is already array if i make $catE = [...] will be array in array right? why would i make that?

    – mafortis
    Nov 16 '18 at 9:09











  • Yes your right it should be $catE

    – Josh
    Nov 16 '18 at 9:12










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329892%2fusing-preg-replace-in-laravel%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














I see a couple of issues with your code that I think will help with getting the solution that you are after.



First like mentioned by @hktang said in his comment you are duplicating your variables and then assigning it's value over and over again instead of adding to it. Try this:



$catE = ;
foreach($catC as $catD)
$catE = [
$cattitle = $catD->title,
$catmeta = $catD->meta_tags,
$catdesc = $catD->meta_description
];



Second you are also resetting the value of $myCustom with each loop of the for each. Try this:



$myCustom = 
foreach($seotemplates as $seotemplate)
$myCustom = str_replace($a1, $catE, $seotemplate->categories_desc_template);



This should result in you getting an array of seotemplates with the values replaced but I have not testing this.






share|improve this answer

























  • regarding to $catE i'm getting Array to string conversion and i think $myCustom isn't right either should be equal to ; or '';

    – mafortis
    Nov 16 '18 at 8:47











  • Yeah sorry you are right. I have updated my code.

    – Josh
    Nov 16 '18 at 8:50











  • shouldn't i get catE in str_replace istead of catD?

    – mafortis
    Nov 16 '18 at 9:08











  • and oh, $catE is already array if i make $catE = [...] will be array in array right? why would i make that?

    – mafortis
    Nov 16 '18 at 9:09











  • Yes your right it should be $catE

    – Josh
    Nov 16 '18 at 9:12















0














I see a couple of issues with your code that I think will help with getting the solution that you are after.



First like mentioned by @hktang said in his comment you are duplicating your variables and then assigning it's value over and over again instead of adding to it. Try this:



$catE = ;
foreach($catC as $catD)
$catE = [
$cattitle = $catD->title,
$catmeta = $catD->meta_tags,
$catdesc = $catD->meta_description
];



Second you are also resetting the value of $myCustom with each loop of the for each. Try this:



$myCustom = 
foreach($seotemplates as $seotemplate)
$myCustom = str_replace($a1, $catE, $seotemplate->categories_desc_template);



This should result in you getting an array of seotemplates with the values replaced but I have not testing this.






share|improve this answer

























  • regarding to $catE i'm getting Array to string conversion and i think $myCustom isn't right either should be equal to ; or '';

    – mafortis
    Nov 16 '18 at 8:47











  • Yeah sorry you are right. I have updated my code.

    – Josh
    Nov 16 '18 at 8:50











  • shouldn't i get catE in str_replace istead of catD?

    – mafortis
    Nov 16 '18 at 9:08











  • and oh, $catE is already array if i make $catE = [...] will be array in array right? why would i make that?

    – mafortis
    Nov 16 '18 at 9:09











  • Yes your right it should be $catE

    – Josh
    Nov 16 '18 at 9:12













0












0








0







I see a couple of issues with your code that I think will help with getting the solution that you are after.



First like mentioned by @hktang said in his comment you are duplicating your variables and then assigning it's value over and over again instead of adding to it. Try this:



$catE = ;
foreach($catC as $catD)
$catE = [
$cattitle = $catD->title,
$catmeta = $catD->meta_tags,
$catdesc = $catD->meta_description
];



Second you are also resetting the value of $myCustom with each loop of the for each. Try this:



$myCustom = 
foreach($seotemplates as $seotemplate)
$myCustom = str_replace($a1, $catE, $seotemplate->categories_desc_template);



This should result in you getting an array of seotemplates with the values replaced but I have not testing this.






share|improve this answer















I see a couple of issues with your code that I think will help with getting the solution that you are after.



First like mentioned by @hktang said in his comment you are duplicating your variables and then assigning it's value over and over again instead of adding to it. Try this:



$catE = ;
foreach($catC as $catD)
$catE = [
$cattitle = $catD->title,
$catmeta = $catD->meta_tags,
$catdesc = $catD->meta_description
];



Second you are also resetting the value of $myCustom with each loop of the for each. Try this:



$myCustom = 
foreach($seotemplates as $seotemplate)
$myCustom = str_replace($a1, $catE, $seotemplate->categories_desc_template);



This should result in you getting an array of seotemplates with the values replaced but I have not testing this.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 16 '18 at 9:11

























answered Nov 16 '18 at 5:16









JoshJosh

718217




718217












  • regarding to $catE i'm getting Array to string conversion and i think $myCustom isn't right either should be equal to ; or '';

    – mafortis
    Nov 16 '18 at 8:47











  • Yeah sorry you are right. I have updated my code.

    – Josh
    Nov 16 '18 at 8:50











  • shouldn't i get catE in str_replace istead of catD?

    – mafortis
    Nov 16 '18 at 9:08











  • and oh, $catE is already array if i make $catE = [...] will be array in array right? why would i make that?

    – mafortis
    Nov 16 '18 at 9:09











  • Yes your right it should be $catE

    – Josh
    Nov 16 '18 at 9:12

















  • regarding to $catE i'm getting Array to string conversion and i think $myCustom isn't right either should be equal to ; or '';

    – mafortis
    Nov 16 '18 at 8:47











  • Yeah sorry you are right. I have updated my code.

    – Josh
    Nov 16 '18 at 8:50











  • shouldn't i get catE in str_replace istead of catD?

    – mafortis
    Nov 16 '18 at 9:08











  • and oh, $catE is already array if i make $catE = [...] will be array in array right? why would i make that?

    – mafortis
    Nov 16 '18 at 9:09











  • Yes your right it should be $catE

    – Josh
    Nov 16 '18 at 9:12
















regarding to $catE i'm getting Array to string conversion and i think $myCustom isn't right either should be equal to ; or '';

– mafortis
Nov 16 '18 at 8:47





regarding to $catE i'm getting Array to string conversion and i think $myCustom isn't right either should be equal to ; or '';

– mafortis
Nov 16 '18 at 8:47













Yeah sorry you are right. I have updated my code.

– Josh
Nov 16 '18 at 8:50





Yeah sorry you are right. I have updated my code.

– Josh
Nov 16 '18 at 8:50













shouldn't i get catE in str_replace istead of catD?

– mafortis
Nov 16 '18 at 9:08





shouldn't i get catE in str_replace istead of catD?

– mafortis
Nov 16 '18 at 9:08













and oh, $catE is already array if i make $catE = [...] will be array in array right? why would i make that?

– mafortis
Nov 16 '18 at 9:09





and oh, $catE is already array if i make $catE = [...] will be array in array right? why would i make that?

– mafortis
Nov 16 '18 at 9:09













Yes your right it should be $catE

– Josh
Nov 16 '18 at 9:12





Yes your right it should be $catE

– Josh
Nov 16 '18 at 9:12



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329892%2fusing-preg-replace-in-laravel%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

政党

天津地下鉄3号線